代码之家  ›  专栏  ›  技术社区  ›  Svelt

Mongodb更新函数仅在包含.then()时起作用

  •  4
  • Svelt  · 技术社区  · 7 年前

    代码正在运行,但我有兴趣理解为什么需要包含.then()。适配器函数以任意一种方式调用,但只有在函数调用后包含.then()时,更新才会显示在db中。

    更新功能:

    updateRequestCount: (id) => {
        return Entry.updateOne({id: id }, { '$inc': { requestCount: 1 } });
    }
    

    updateRequestCount(request.query.id)
    .then();
    

    updateRequestCount(request.query.id);
    
    1 回复  |  直到 7 年前
        1
  •  3
  •   diegoteixeir4    7 年前

    Model.updateOne() 或Mongoose上模型的任何其他CRUD方法,它返回 Query 具有 then() 方法,该方法将执行查询并返回 Promise .

    因此,在调用时不会立即执行查询 updateOne() 然后() 在返回的查询对象上。

    updateOne() 然后() :

    updateRequestCount: (id) => {
      return Entry.updateOne({id: id }, { '$inc': { requestCount: 1 } }, err => {
        // ...
      });
    }