【发布时间】:2021-11-15 07:58:54
【问题描述】:
从 mongoose 5.x 迁移到 6.x 时,打字稿出现错误,特别是 UpdateQuery $inc,因为类型设置为未定义。我使用的是内置类型而不是 @types/mongoose 这是一个示例:
export interface PrintSettings extends Document {
_id: String
currentBatch: Number
}
const PrintSettingsSchema: Schema<PrintSettings> = new Schema(
{
_id: { type: String },
currentBatch: { type: Number, default: 0, min: 0 },
},
{ timestamps: true }
)
let inc = await PrintSettingsModel.findOneAndUpdate(
{ _id: settingsId },
{ $inc: { currentBatch: 1 } }, // <------ Here is where the error occurs
{ useFindAndModify: false, new: true }
)
我收到的错误是这样的:
No overload matches this call.
Overload 1 of 3, '(filter: FilterQuery<PrintSettings>, update: UpdateQuery<PrintSettings>, options: QueryOptions & { rawResult: true; }, callback?: ((err: CallbackError, doc: any, res: any) => void) | undefined): Query<...>', gave the following error.
Type '{ currentBatch: number; }' is not assignable to type 'undefined'.
Overload 2 of 3, '(filter: FilterQuery<PrintSettings>, update: UpdateQuery<PrintSettings>, options: QueryOptions & { upsert: true; } & ReturnsNewDoc, callback?: ((err: CallbackError, doc: PrintSettings, res: any) => void) | undefined): Query<...>', gave the following error.
Type '{ currentBatch: number; }' is not assignable to type 'undefined'.
Overload 3 of 3, '(filter?: FilterQuery<PrintSettings> | undefined, update?: UpdateQuery<PrintSettings> | undefined, options?: QueryOptions | null | undefined, callback?: ((err: CallbackError, doc: PrintSettings | null, res: any) => void) | undefined): Query<...>', gave the following error.
Type '{ currentBatch: number; }' is not assignable to type 'undefined'.ts(2769)
index.d.ts(2576, 5): The expected type comes from property '$inc' which is declared here on type 'UpdateQuery<PrintSettings>'
这是 mongoose 6 的类型错误还是更新或原子增量已更改?
【问题讨论】:
-
您是使用内置的
index.d.ts还是使用@types/mongoose?官方index.d.ts确实包括$inc。你能检查 node_modules mongoose 中的那个文件,看看那一行是否存在? -
我只是使用内置类型而不是 @types/mongoose
-
如果你进入 node_modules mongoose,你会在 index.d.ts 中看到那一行吗?
-
是的,我看到那行,文件完全一样
标签: typescript mongodb mongoose node-modules