我试图访问我的两个函数内的变量,但我没有收回任何东西。我在做什么错&我该如何解决它?NodeJS/Javascript:从函数访问变量
因此,这里是我的代码是什么样子:
exports.show = function(req, res) {
var next_id;
var prev_id;
Article.find({'created':{$gt:req.article.created}}).sort('-created').limit(1).exec(function(err, articles) {
if (!err && articles[0] !== undefined) {
next_id = articles[0].id; // trying to get this variable
}
});
Article.find({'created':{$lt:req.article.created}}).sort('-created').limit(1).exec(function(err, articles) {
if (!err && articles[0] !== undefined) {
prev_id = articles[0].id; // and this variable
}
});
res.jsonp({article : req.article, next_href : next_id, prev_href : prev_id});
};
'Article.find'异步吗? – thefourtheye
@thefourtheye我真的不确定(我是NodeJS的新手,并且仍然是Javascript中的新手) – goo