【发布时间】:2019-11-05 13:26:03
【问题描述】:
我正在通过我正在学习的课程学习一些 JavaScript 后端编程。它专注于 ExpressJS、MongoDB 和 GraphQL。因为我喜欢让自己的事情更具挑战性,所以我决定在完成所有 TypeScript 课程的同时复习我的 TypeScript。
无论如何,我使用的是 mongoose 和 @types/mongoose 的 5.5.6 版本。这是我的数据库记录类型的界面:
export default interface IEvent {
_id: any;
title: string;
description: string;
price: number;
date: string | Date;
}
然后我像这样创建猫鼬模型:
import { Document, Schema, model } from 'mongoose';
import IEvent from '../ts-types/Event.type';
export interface IEventModel extends IEvent, Document {}
const eventSchema: Schema = new Schema({
title: {
type: String,
required: true
},
description: {
type: String,
required: true
},
price: {
type: Number,
required: true
},
date: {
type: Date,
required: true
}
});
export default model<IEventModel>('Event', eventSchema);
最后,我为 GraphQL 突变编写了以下解析器:
createEvent: async (args: ICreateEventArgs): Promise<IEvent> => {
const { eventInput } = args;
const event = new EventModel({
title: eventInput.title,
description: eventInput.description,
price: +eventInput.price,
date: new Date(eventInput.date)
});
try {
const result: IEventModel = await event.save();
return { ...result._doc };
} catch (ex) {
console.log(ex); // tslint:disable-line no-console
throw ex;
}
}
我的问题是 TypeScript 给了我一个错误,即“._doc”不是“结果”的属性。确切的错误是:
error TS2339: Property '_doc' does not exist on type 'IEventModel'.
我不知道我做错了什么。我已经多次查看文档,似乎我应该在这里拥有所有正确的 Mongoose 属性。暂时我将把属性添加到我自己的界面中,只是为了继续课程,但我更希望在这里帮助确定正确的解决方案。
【问题讨论】:
标签: mongodb typescript mongoose