Currently, I am enhancing my JavaScript backend programming skills through a comprehensive course that covers ExpressJS, MongoDB, and GraphQL. To make things more challenging, I have decided to reinforce my TypeScript proficiency by completing all the assignments using TypeScript.
At the moment, I am utilizing version 5.5.6 of mongoose and @types/mongoose. Below is the interface I have defined for the type of DB record:
export default interface IEvent {
_id: any;
title: string;
description: string;
price: number;
date: string | Date;
}
Subsequently, I crafted the Mongoose Model in the following manner:
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);
Lastly, I have formulated the subsequent resolver for a GraphQL mutation:
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;
}
}
The issue I am encountering is an error in TypeScript stating that "._doc" is not a property on "result". The specific error message reads as follows:
error TS2339: Property '_doc' does not exist on type 'IEventModel'.
I am struggling to identify where I might be going wrong. Despite thoroughly reviewing the documentation, it appears that all the necessary Mongoose properties are included. I may temporarily add the property to my own interface to progress with the course; however, I would greatly appreciate assistance in pinpointing the correct resolution.