【问题标题】:mongoose async callback undefined猫鼬异步回调未定义
【发布时间】:2016-04-03 19:17:48
【问题描述】:

我使用“猫鼬”和“异步”。我的搜索也有效,但在最后一次回调时,我的数据不会被传递。变量始终未定义。在猫鼬的文档中,我没有找到线索。我已经在代码中标记了提到的位置。

async.whilst(
    function() {
      return noresult
    },
    function(callback) {
      if (day >= 7 && newdate >= 1440) {
        noresult = false;
        callback("Nothing", newquery);

      } else if (day < 7 && newdate >= 1440) {
        day++;
        newdate = 0;
        delete newquery['search.'
            ' + d[day- 1]];
            delete newquery['search.
              ' + d[day - 1] + '
              b '];
              newquery['search.' + d[day]] = {
                $lte: newdate
              }; newquery['search.' + d[day] + 'b'] = {
                $gte: newdate
              }; console.log("1 " + day + " " + newdate);
            } else if (day >= 7 && newdate < 1440) {
              newdate++;
              console.log("2 " + day + " " + newdate);
            } else if (day <= 7 && newdate < 1440) {
              newdate++;
              delete newquery['search.' + d[day - 1]];
              delete newquery['search.' + d[day - 1] + 'b'];
              newquery['search.' + d[day]] = {
                $lte: newdate
              };
              newquery['search.' + d[day] + 'b'] = {
                $gte: newdate
              };
            }

            Data.find(newquery, function(err, newresult) {
              if (err) {
                callback(err);
              }
              if (!newresult.length) {
                noresult = true;
                if (day >= 7 && newdate >= 1440) {
                  callback("Nothing found");
                } else {
                  callback(null, newquery);
                }
              } else {
                noresult = false;
                callback(null, newresult);
              }
            });


          },
          function(err, newresult) {
            if (err) {
              return res.status(404).send(err);
            }
            // At this point newresult is undefined
            return res.send(newresult);

          }
      );

【问题讨论】:

    标签: javascript node.js mongodb asynchronous mongoose


    【解决方案1】:

    我查看了 async.whilst 函数的实现,它不会为最终回调生成最后一个结果。所以这可能对文档有一些误导。

    我已经实现了我自己的同时满足你的需要:

    var whilst = function (test, iterator, callback, prevResult) { 
        if (test()) { 
            iterator(function (err, result) { 
                if (err) { 
                    return callback(err); 
                } 
                whilst(test, iterator, callback, result); 
            }); 
        } 
        else { 
            callback(null, prevResult); 
        } 
    };
    

    尝试使用它而不是 async.whilst。让我知道它是否对你有用。

    更新:

    我发现实际上有一个 issue 关于这种误导,因此它将在较新版本的异步库中得到修复。

    【讨论】:

    • 我了解代码,但不知道如何在我的示例中使用它。异步有一个错误是非常不幸的。
    • @Jack 不要使用 async.whilst 函数,只需声明并使用我们的 while 函数。您的其余代码保持不变。如果你愿意,你可以使用我们的 while 函数使用 lodash mixin 来扩展异步对象。
    • 我知道,但我没有让它运行。
    • 你能帮我或在 JSFiddle 上创建一个小例子吗?
    • 是的,看看这个例子:jsfiddle.net/5gvfg37h。如果您对此有任何疑问,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 2021-04-20
    • 2014-11-16
    • 2019-10-12
    • 2018-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多