简体   繁体   English

Mongoos `findOneAndUpdate` 返回 `QueryWithHelpers` 而不是结果

[英]Mongoos `findOneAndUpdate` returns `QueryWithHelpers` instead of result

I have the following invocation.我有以下调用。

  static findOneAndUpdate(
    filter: FilterQuery<ISyncedOrder>,
    update: UpdateQuery<ISyncedOrder>
  ): Promise<ISyncedOrder> {
    return SyncedOrders.findOneAndUpdate(
      filter,
      update,
      {upsert: true}
    );
  }

According to documents, I should be getting a document.根据文件,我应该得到一份文件。 But, IDE shows an error of a type mismatch (screenshot below).但是,IDE 显示类型不匹配的错误(下面的屏幕截图)。

在此处输入图像描述

When I checked the node module, I see the following signature for the method findOneAndUpdate .当我检查节点模块时,我看到了findOneAndUpdate方法的以下签名。

在此处输入图像描述

What am I missing?我错过了什么? Looks like I am headed in a wrong direction.看起来我走错了方向。

The issue occurs because "Mongoose queries are not promises" .出现此问题是因为"Mongoose queries are not promises"

They support just enough to be used as one (it's a "thenable" ), but not enough to fool TypeScript into believing it's actually a promise.他们支持的只是足以用作一个(它是一个“thenable” ),但不足以让 TypeScript 相信它实际上是一个 promise。

The Mongoose documentation also suggests how to get a real promise: by using Query.exec() : Mongoose 文档还建议如何使用Query.exec()获得真正的 promise:

return SyncedOrders.findOneAndUpdate(
  filter,
  update,
  {upsert: true}
).exec();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM