【问题标题】:Having problems copying a document into another collection mongoose将文档复制到另一个集合 mongoose 时出现问题
【发布时间】:2019-05-29 21:15:11
【问题描述】:

这是我用来查找需要复制到其他集合的文档的代码:

public updateConfigWithType(req: Request, res: Response) {
  configs.findOne({'companyInfo.uniquecompanyid': req.params.id}, function(err, config){
      if (err) {
        return res.send(err);
      }
      let swap = new (oldConfigs.model('oldConfigs'))(config)
      swap.save()

      config.parserConfig = req.body;

      config.save(err => {
        if (err) return res.send(err);
          res.json({ data: config });
      });
  });
}


The error message is the following:
 (node:65757) UnhandledPromiseRejectionWarning: DocumentNotFoundError: No document found for query "{ _id: {} }"
 (node:65757) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
10:34:34 AM web.1 |  (node:65757) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

不知道我将如何解决这个问题。我也得到两个不同的错误?我在这里缺少什么,请解释一下。

【问题讨论】:

    标签: typescript express mongoose


    【解决方案1】:

    这是相关的 2 个错误。我认为错误来自swap.save(),因为在猫鼬中,如果没有回调作为参数传递,则该方法返回一个 Promise

    您应该检查您传入let swap = new (oldConfigs.model('oldConfigs'))(config)config 对象,因为它会引发错误。

    第二条消息是因为您没有捕捉到 promise 错误。你可以这样写:

    swap.save().catch(console.error)
    

    【讨论】:

    • 感谢您的回答。就在你写之前,我实际上尝试了这个swap.save().then(console.log("swap complete")).catch(err => console.log(err));you should check the config object you pass in 到底是什么意思?检查什么?
    • 承诺返回此错误DocumentNotFoundError: No document found for query "{ _id: {} }"。我不知道你传入的参数是哪个配置,但它可能来自这个对象。顺便问一下,swap.save() 的结果是什么?
    猜你喜欢
    • 2016-11-07
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-23
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    相关资源
    最近更新 更多