【问题标题】:How to solve Circular dependency error in nodejs?如何解决nodejs中的循环依赖错误?
【发布时间】:2019-08-07 06:46:24
【问题描述】:

我在 post 请求中执行以下代码:

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

请让我知道这里可能存在什么问题以及如何纠正它。

【问题讨论】:

    标签: node.js mongodb typescript


    【解决方案1】:

    函数insertOne() 不返回_id,它返回InsertOneWriteOpResult。这有一个 connection 字段,可能会有循环引用。

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

    【讨论】:

      猜你喜欢
      • 2021-05-23
      • 2022-09-22
      • 1970-01-01
      • 2023-01-30
      • 2012-03-15
      • 2016-09-21
      • 2020-11-12
      相关资源
      最近更新 更多