Mongoose promises documentation says queries are not promises?

后端 未结 3 587
庸人自扰
庸人自扰 2020-12-10 19:11

From the docs (Mongoose v5.4.1, latest version):

Mongoose async operations, like .save() and queries, return thenables. This

3条回答
  •  盖世英雄少女心
    2020-12-10 19:36

    From the documentation:

    Mongoose queries are not promises. They have a .then() function for co and async/await as a convenience. However, unlike promises, calling a query's .then() can execute the query multiple times.

    So unlike an actual promise, if you call then() multiple times on the query, you actually execute the query (or update) multiple times.

    If you want an actual promise, call exec() on the query.

    let promise = Test.findOne({}).exec();
    

提交回复
热议问题