问题描述
在意识到 data = [
{'column_1': dict_['column1'],'column_2': dict_['column2'},]
具有类型信息后,我试图放弃使用 @types/mongoose
。
但是,我现在在以下代码上运行 mongoose 5.11
时遇到了 Typescript 问题:
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
解决方法
是的,缺少类型信息。
@types/mongoose@5.10.3
defines [Symbol.asyncIterator]
on DocumentQuery
和 Query
扩展 DocumentQuery
。
mongoose@5.11.14
does not define [Symbol.asyncIterator]
on Query
。
必须在 for await...of
语句右侧使用的任何接口上定义此方法。
我注意到两个包都定义了一个扩展 stream.Readable
的 QueryCursor
。这个类确实定义了一个[Symbol.asyncIterator]
方法,cursor()
method of the Query
interface返回一个QueryCursor
的实例。在不使用 @types/mongoose
的情况下,尝试查看以下内容是否按预期编译和运行:
for await (const document of db.myModel.find().cursor()) {
// ^^^^^^^^^
...
}