在意识到@types/mongoose有类型信息之后,我正在尝试放弃使用mongoose 5.11。
但是,在对以下代码运行tsc时,我现在遇到了类型转换问题:
for await (const document of db.myModel.find()) {
...
}错误是:
Type 'Query<IMyDocumentType[], IMyDocumentType>' must have a '[Symbol.asyncIterator]()' method that returns an async iterator.忽略错误并运行代码可以正常工作。我是遗漏了什么,还是mongoose 5.11.14中缺少了@types/mongoose所拥有的类型信息?
mongoose 5.11.14
node.js 12.19.0发布于 2021-01-29 18:04:38
是的,缺少类型信息。
@types/mongoose@5.10.3 defines [Symbol.asyncIterator] on DocumentQuery和Query扩展了DocumentQuery。
mongoose@5.11.14 does define [Symbol.asyncIterator] on Query。
必须在for await...of语句的右侧使用的任何接口上定义此方法。
我确实注意到,这两个包都定义了扩展QueryCursor的stream.Readable。这个类确实定义了一个[Symbol.asyncIterator]方法,cursor() method of the Query interface返回QueryCursor的一个实例。在不使用@types/mongoose的情况下,试着查看以下内容是否按您的预期进行编译和运行
for await (const document of db.myModel.find().cursor()) {
// ^^^^^^^^^
...
}https://stackoverflow.com/questions/65959353
复制相似问题