将文档复制到另一个猫鼬时遇到问题

时间:2019-01-03 09:38:56

标签: typescript express mongoose

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

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.

不确定我将如何解决此问题。我也得到两个不同的错误?我在这里想念的是什么,请解释一下。

1 个答案:

答案 0 :(得分:0)

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

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

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

swap.save().catch(console.error)
相关问题