Howdy everyone, I've encountered an issue stating "Property '_doc' does not exist on type 'User & { _id: ObjectId; }'" in one of my controller documents while trying to fetch a particular user. My backend database is implemented using Mongoose + Typescript. Initially, my interface did not extend the Document class. Following some research, I discovered that extending my interface to the Document class was necessary. However, I'm stuck on retrieving my user's information.
Here are my current User model (left) and controller (right) files: https://i.sstatic.net/9co2j.jpg
UPDATE: I made a small modification by adding the following to my model file:
import mongoose, { Schema, model } from 'mongoose';
export interface UserResult<T> extends mongoose.Document {
_doc: T;
}
export interface User extends UserResult<User> {
username: string;
email: string;
password: string;
};
const UserSchema = new Schema<User>({
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true }
});
export default model<User>('User', UserSchema);
This adjustment resolved the previous error, but now I'm encountering a new one: "Property 'password' does not exist on type 'User | undefined'". The password property definitely exists in my User model. Any suggestions for quick fixes?
Below is the updated setup after addressing the initial bug: https://i.sstatic.net/qiqY0.jpg