2012-07-13 128 views
1

這是我的代碼,它的所有工作都是return_info沒有得到安裝的例外。是否可以從請求函數調用的回調中訪問父return_info變量?從被調用函數的回調函數中訪問函數變量

module.exports = { 
    fetch_template_by_id : function(id) { 
    var request = require('request'); 
    var return_info = {}; //TO BE MODIFIED 
    request('http://localhost:5000/templates/v1/template/' + id, function (error, response, body) { 
     if (!error && response.statusCode == 200) { 
     console.log("Success - Error = " + error + " | Response = " + response + " | Body = " + body); 
     return_info.body = body;     // Should Modify the top 
     return_info.json = JSON.parse(body);  // Should Modify the top 
     return_info.success = true;    // Should Modify the top 
     } else { 
     console.error("There was an error requesting template ID=" + id) 
     return_info.error = error;    // Should Modify the top 
     return_info.response = response;   // Should Modify the top 
     return_info.success = false;    // Should Modify the top 
     } 
    }); 
    return return_info; 
    } 
} 

編輯:進一步參考這是調用它的代碼。

app.get('/form', function (req, res) { 
var template_helper = require('../services/template-helper'); 
var template = template_helper.fetch_template_by_id("BirthRecord"); 
console.log(template); 
if (template.success === true) { 
    res.render('form', {"template": template.json }); 
} else { 
    res.render('index'); 
} 
}); 

回答

0

由於請求是異步的,它會在您返回return_info後執行。所以,你需要提供以下

module.exports = { 
    fetch_template_by_id : function(id, cb) { 
    var request = require('request'); 
    var return_info = {}; //TO BE MODIFIED 
    request('http://localhost:5000/templates/v1/template/' + id, function (error, response, body) { 
     if (!error && response.statusCode == 200) { 
     console.log("Success - Error = " + error + " | Response = " + response + " | Body = " + body); 
     return_info.body = body;     // Should Modify the top 
     return_info.json = JSON.parse(body);  // Should Modify the top 
     return_info.success = true;    // Should Modify the top 
     } else { 
     console.error("There was an error requesting template ID=" + id) 
     return_info.error = error;    // Should Modify the top 
     return_info.response = response;   // Should Modify the top 
     return_info.success = false;    // Should Modify the top 
     } 
     cb(return_info); 
    }); 
    } 
} 

fetch_template_by_id('awesome', function (info) { 
    console.log(info); 
}); 

編輯回調函數作爲展示 - 隨着上下文

你需要沿着回調的方式想,如果你想開始工作的Node.js ,下面是你應該怎麼做的例子。

app.get('/form', function (req, res) { 
    var template_helper = require('../services/template-helper'); 
    template_helper.fetch_template_by_id("BirthRecord", function (template) { 
    console.log(template); 
    if (template.success === true) { 
     res.render('form', {"template": template.json }); 
    } else { 
     res.render('index'); 
    } 
    }); 
}); 
+0

這會行得通,但我不需要打破產品的流動,儘管我對node.js是新手,所以我不知道我能做什麼。我發佈了調用這個的代碼,只是爲了獲得更多的上下文。你的解決方案可能仍然有幫助,我只是不知道有關節點發布。 – 2012-07-13 16:47:28

+0

我編輯過,請檢查。你需要在使用nodejs異步使用之前閱讀如何使用回調代碼。 – 2012-07-13 17:41:17

+0

所以我剛剛提交了一些代碼,看起來幾乎與上面的代碼完全相同,並且工作完美。在您讓我進入正確的思維模式之前,您的幫助感謝您確認我的思維過程。謝謝。 – 2012-07-13 19:37:50

相關問題