【发布时间】:2023-01-23 22:18:12
【问题描述】:
我想创建一个 mongoose 模式,我正在尝试向文档添加一个名为 start 的新属性。它在 javascript 中工作,但在 typescript 中,我收到错误消息“类型‘Query<any, any, {}, any>’.ts(2339) 上不存在属性‘start’”。
感谢您为修复错误提供的任何帮助。
import mongoose from 'mongoose';
interface tourSchemaTypes {
name: string;
}
const tourSchema = new mongoose.Schema<tourSchemaTypes>({
name: {
type: String,
required: [true, 'A tour must have a name'],
unique: true,
},
});
const Tour = mongoose.model<tourSchemaTypes>('Tour', tourSchema);
tourSchema.pre(/^find/, function (next) {
this.find({ secretTour: { $ne: true } });
this.start = Date.now();
next();
});
tourSchema.post(/^find/, function (docs, next) {
console.log(`Query took ${Date.now() - this.start} milliseconds`);
console.log(docs);
next();
});
【问题讨论】:
标签: javascript node.js typescript mongoose