【问题标题】:async await node.js skips the second await statement save mongodbasync await node.js 跳过第二个 await 语句保存 mongodb
【发布时间】:2018-05-07 12:51:30
【问题描述】:

我在 node.js ES6 中使用 await async ...

async insertIngot(body, callback) {

    console.log('*** ItemsRepository.insertIngot');

    console.log(body);

    const data = await this.getItemsTest();
    console.log('*** getItems ok');
    items = data.items;

    let item = new Ingot();

    item.ingotName = body.ingotName;
    item.formulaName = body.formulaName;
    item.items = items;

    await item.save();

    return item;
}

第一个 await 有效,然后它会跳过其余代码并引发错误:

(node:16612) DeprecationWarning: Mongoose: mpromise(mongoose 的默认承诺库)已弃用,请插入您自己的承诺库:http://mongoosejs.com/docs/promises.html

【问题讨论】:

  • 仅供参考,async/await 不是 ES6 的一部分。

标签: javascript node.js mongodb async.js ecmascript-2017


【解决方案1】:

在 mongoose 中,您需要定义要使用的 Promise 类型。你正在使用内置的 es6 承诺,所以要告诉 mongoose,你需要这样做:

mongoose.Promise = global.Promise;

这里是文档:http://mongoosejs.com/docs/promises.html#plugging-in-your-own-promises-library

【讨论】:

  • 我知道为什么它会跳过其余的代码。这只是一个警告。一个想法?
  • 谢谢,试试这个。
  • @GrégoryNEUT 我不知道我们是否可以确定它正在跳过,发布的代码中没有任何调试,另外,错误被抛出save(),所以我们知道它正在尝试保存。由于被抛出的消息,它可能没有保存。
  • @GetOffMyLawn 够公平,很有趣。谢谢:)
  • @Danish 我认为您可能需要将async insertIngot 更改为async function insertIngot
猜你喜欢
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 2019-01-29
  • 2018-04-12
  • 2019-09-22
  • 1970-01-01
  • 2020-03-25
  • 2021-12-31
相关资源
最近更新 更多