简体   繁体   English

函数调用Javascritpt的未定义返回值

[英]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. 我需要使用此返回值来与其他函数进行验证,但它似乎在范围之内。

This is my code: 这是我的代码:

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);
    });
};

Move the following line: 移动以下行:

res.render("index.html", content);

into the hasNextPage callback: 进入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);

    });
  });
};

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. 如果您希望getRecentPost返回某些内容,则还需要向其添加一个回调,以便可以使用它的返回值。 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);

into this: 到这个:

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));
}

asynchronous code looks like this: 异步代码如下所示:

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. 请注意,您不返回,而是传递了一个回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM