来自函数调用 Javascriptpt 的未定义返回值 [英] Undefined return value from the function call Javascritpt
问题描述
我正在尝试获取返回值,但总是未定义.
I'm trying to get the return value, but always get undefined.
var hasNext;
es.search(nextQuery, function (err, data) {
if(data.hits.hits.length) {
return hasNext = true;
}
return hasNext = false;
});
我不确定如何获得返回值并在其他地方使用它?我需要使用任何这个返回值来验证其他函数,但它似乎在范围内什么的.
I'm not sure how can I get whatever the return value and use it somewhere else? I need to use whatever this return value to validate with other functions, but it seems to be in the scope or something.
这是我的代码:
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);
});
};
推荐答案
移动以下行:
res.render("index.html", content);
进入hasNextPage
回调:
functions.getRecentPost = function ( req, res, next ) {
//.........
es.search(query, function (err, data) {
//.........
hasNextPage(_page, content.posts.length, function (data) {
content.hasNext = data;
res.render("index.html", content);
});
});
};
如果您希望 getRecentPost
返回一些东西,那么您还需要向它添加一个回调,以便您可以使用它的返回值.例如,如果您希望这样做:
If you expect getRecentPost
to return something then you need to add a callback to it as well so that you can use it's return value. For example, if you expect to be doing this:
functions.getRecentPost = function ( req, res, next) {
//......
return content;
}
doSomething(functions.getRecentPost(x,y,z));
它不会工作,因为内容的最终值将被异步检索.相反,您需要这样做:
It won't work because the final value of content will be retrieved asynchronously. Instead you need to do this:
functions.getRecentPost = function ( req, res, next, callback ) {
//.........
hasNextPage(_page, content.posts.length, function (data) {
content.hasNext = data;
res.render("index.html", content);
callback(content);
});
};
functions.getRecentPost(x,y,z,function(content){
doSomething(content);
})
不能异步返回数据.你需要改变你的代码(和你的想法),不要写这样的东西:
You cannot return data asynchronously. You need to change your code (and your thinking) from writing stuff like this:
asyncFunction(function(data){
foo = data;
});
doSomething(foo);
进入这个:
asyncFunction(function(data){
doSomething(data);
});
基本上,将您希望在异步函数之后运行的所有代码移动到您传递给它的回调函数中.
Basically, move all the code that you would want to run after the async function into the callback function you passed into it.
常规命令式代码如下所示:
Regular imperative code looks like this:
function fN () {
x = fA();
y = fB(x);
z = fC(y);
return fD(fE(z));
}
异步代码如下所示:
function fN (callback) {
fA(function(x){
fB(x,function(y){
fC(y,function(z){
fE(z,function(zz){
fD(zz,function(zzz){
callback(zzz);
});
});
});
});
});
}
注意你没有返回,而是传入一个回调函数.
Notice that you don't return, you pass in a callback instead.
这篇关于来自函数调用 Javascriptpt 的未定义返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!