没有保存相同的文档,但是“ ParallelSaveError:无法并行保存()相同的文档多次”。发生?

时间:2019-10-12 16:41:07

标签: javascript node.js mongodb mongoose

我异步保存两个单独的模型,但出现此错误。

删除第二个保存后,它仍然会发出此错误。即使返回.save()。then(),现在也会返回此错误。

const saveLinkedAccounts = async (acc1, acc2) => { //application requires to save two accounts quite often (account linking)
  try {
    const save1 = acc1.save(); // Moving await from the return to before the acc1.save() doesn't work
    //acc1.save().then(res1 => { //Doesn't work too
    //  return res1;
    });

    const save2 = acc2.save();

    return { res1: await save1, res2: await save2 };
  } catch (err) {
    console.log(err);
    throw err;
  }

Atleast删除第二个保存应该已经解决了吗?我再也不确定了。在此之前,我不调用任何.save()函数。该函数被调用一次。此函数嵌套在

              // prettier-ignore
              const SC_User  =  createUser({type: 'SCUser', carrier : 'discord', profile : profile, refreshToken : refreshToken, accessToken : accessToken})
              let foundOtherUser = false;

              DB_COLLECTIONS.forEach(dbUser => {
                let user = eval(dbUser);
                user.findOne({ email: profile.email }, async (err, user) => {
                  if (err) {
                    console.log(err);
                    return done(err);
                  }
                  if (user) {
                    foundOtherUser = await linkAndSaveAccounts(SC_User, user) // function gets called indside this function, there is no .save() or any other database operation
                      .res1;
                  }
                });
              });

              if (foundOtherUser) {
                return done(null, foundOtherUser);
              } else {
                SC_User.save()
                  .then(user => done(null, user))
                  .catch(err => {
                    console.log(err);
                    done(err);
                  });
              }
            }


编辑/更新:将forEach迭代器更改为的效果也不理想。

0 个答案:

没有答案
相关问题