如何解决nodejs中的循环依赖错误?

时间:2019-08-06 22:31:25

标签: node.js mongodb typescript

我正在发布请求中执行以下代码:

try{
  const _id = await db.collection('UserInformation').insertOne(userObj);
  await db.collection('LoggedInUser').updateOne({ userId: _id }, { '$set': { 
  'isLoggedIn': true } }, { upsert: true });  
}
catch(e){
  console.log(e);
}

我收到循环依赖错误。

当我注释掉以下行时,一切正常。

await db.collection('LoggedInUser').updateOne({ userId: _id }, { '$set': { 
  'isLoggedIn': true } }, { upsert: true });

请让我知道这里可能有什么问题以及如何解决。

1 个答案:

答案 0 :(得分:2)

函数insertOne()不返回_id,而是返回InsertOneWriteOpResult。该字段有一个connection字段,该字段可能具有循环引用。

使用结果对象中的insertedId而不是整个对象本身。

相关问题