我正在尝试获取返回值,但总是未定义。
var hasNext;
es.search(nextQuery, function (err, data) {
if(data.hits.hits.length) {
return hasNext = true;
}
return hasNext = false;
});
我不确定如何获得返回值并在其他地方使用它?我需要使用这个返回值来验证其他函数,但它似乎在范围内。
这是我的代码:
functions.getRecentPost = function ( req, res, next ) {
.........
// This will get all the post
es.search(query, function (err, data) {
if(err) { // if error means no post and redirect them to create one
return res.redirect('/new/post');
}
....
content.hasPrevious = hasPreviousPage(_page, content.posts.length); // this function is okay
hasNextPage(_page, content.posts.length, function (data) {
content.hasNext = data;
});
res.render("index.html", content);
});
};
function hasNextPage (pageNum, totalPost, callback) {
es.search(query, function (err, data) {
if(data.hits.hits.length) {
return callback(true);
}
return callback(false);
});
};