【问题标题】:Async.eachSeries on FindOne doesn't workFindOne 上的 Async.eachSeries 不起作用
【发布时间】:2023-03-23 10:31:01
【问题描述】:

我遇到了 FindOne 的异步问题。我有一个名为 idChildren 的 id 数组(如 [ [516152], [158796], [123654, 147852] ]),我想检查数组中当前位置的 id 是否在数据库中。 children 是一个数组,我在其中保存包含所有信息的字符串。

for (var j = 0; j < idChildren.length; j++)
{
    async.eachSeries(idChildren, function() {
        for (var i = 0; i < idChildren[j].length; i++)
        {
            Children.findOne({ _id: idChildren[j] }, function (err, child) {
                console.log("I'm in the findOne");
                if (child != undefined && !err) {
                    if (child.lastname == undefined)
                        tmp2 += ',';
                    else
                        tmp2 += child.lastname + ',';
                    if (child.firstname == undefined)
                        tmp2 += ',';
                    else
                        tmp2 += child.firstname + ',';
                    if (child.birthday == undefined)
                        tmp2 += ',';
                    else
                        tmp2 += child.birthday + ',';
                    tmp2 += '\n';
                }
                children.push(tmp2);
                tmp2 = "";
          });
        }
    });
}
console.log(children);

问题是 "console.log(children)" 打印 [ , , ] 并且在 "I'm in the findOne" 三遍之后。我认为这是我的 async.eachSeries 不正确。 你能帮帮我吗?

编辑

async.eachSeries(idChildren, function(idChild, next) {
    Children.findOne({ _id: idChild }, function (err, child) {
        console.log("I'm in the findOne");
        if (child != undefined && !err) {
            if (child.lastname == undefined)
                tmp2 += ',';
            else
                tmp2 += child.lastname + ',';
            if (child.firstname == undefined)
                tmp2 += ',';
            else
                tmp2 += child.firstname + ',';
            if (child.birthday == undefined)
                tmp2 += ',';
            else
                tmp2 += child.birthday + ',';
            tmp2 += '\n';
        }
        children.push(tmp2);
        tmp2 = "";
        next();
        return;
  });
});

console.log(children);

输出是:

[]
我在 findOne
我在 findOne
我在 findOne
我在 findOne
我在 findOne

因此,console.log(children) 会在 FindOne 循环运行之后首先执行。 如果console.log(children)在findOne结尾,则数组是正确的。

【问题讨论】:

  • 我编辑了我的答案

标签: javascript node.js mongodb asynchronous mongoose


【解决方案1】:

所以,如果我理解正确,代码可以正常工作,问题只出在 console.log() 的时间以及您想要在循环结束时执行的任何其他逻辑。

如果是这种情况,请使用以下代码:

async.eachSeries(idChildren, function(idChild, next) {
    Children.findOne({ _id: idChild }, function (err, child) {
        console.log("I'm in the findOne");
        if (child != undefined && !err) {
            if (child.lastname == undefined)
                tmp2 += ',';
            else
                tmp2 += child.lastname + ',';
            if (child.firstname == undefined)
                tmp2 += ',';
            else
                tmp2 += child.firstname + ',';
            if (child.birthday == undefined)
                tmp2 += ',';
            else
                tmp2 += child.birthday + ',';
            tmp2 += '\n';
        }
        children.push(tmp2);
        tmp2 = "";
        next();
        return;
  });
},
// finally method
function(err) {
    if (err) {
        //do some error handling
    }

    console.log(children);
    // do some other logics
});

如您所见,async.eachSeries 方法接受另一个代表“finally 方法”的函数参数——循环完成后将被调用的方法。

如果其中一个循环迭代将调用 next 并带有一个代表错误的参数 - 例如:

return next("Cannot find child object");

在这种情况下 - 循环将停止运行,并且 finally 方法中的“err”参数将包含您作为错误传递的字符串。

如果没有错误,“err”将为空,您可以继续执行您想要的任何逻辑。

【讨论】:

  • 感谢您的回答。不幸的是,我的网络控制台打印:04:27:45 web.1 | [] 04:27:45 web.1 |我在 findOne 04:27:45 web.1 |我在 findOne 04:27:46 web.1 |我在 findOne 04:27:46 web.1 |我在 findOne 04:27:46 web.1 |我在 findOne 所以 children 先打印,然后我在 async.EachSeries 所以我的 children 变量总是空的:/
  • 请附上您的整个代码以及所做的更改以及作为问题的编辑打印的内容,以便我了解究竟是什么问题。
猜你喜欢
  • 2015-04-22
  • 2016-09-18
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多