Typescript is complaining at this line:
user.posts.pull(postId);
I am getting this error:
Property 'pull' does not exist on type 'PostDoc[]'
since postId is received as req.params.postId
it is type string, so i converted it to mongoose objectId but i still have the same error:
user.posts.pull(mongoose.Types.ObjectId(postId));
pull() works in mongoose arrays. this line of code how I implemented in javacsript. I am converting my project to typescript. this is the user interface and schema for user model.
interface UserDoc extends mongoose.Document {
email: string;
password: string;
posts: PostDoc[];
name: string;
status: string;
}
const userSchema = new Schema({
email: { type: String, required: true },
password: { type: String, required: true },
name: { type: String, required: true },
status: { type: String, default: "I am a new user" },
posts: [{ type: Schema.Types.ObjectId, ref: "Post" }],
});
Here post schema and interface
interface PostDoc extends Document {
title: string;
content: string;
imageUrl: string;
creator: Types.ObjectId;
}
const postSchema = new Schema(
{
title: {
type: String,
required: true,
},
imageUrl: {
type: String,
required: true,
},
content: {
type: String,
required: true,
},
creator: {
type: Schema.Types.ObjectId,
ref: "User",
required: true,
},
},
{ timestamps: true }
Property 'pull' does not exist on type 'PostDoc[]'
, it tells you everything you need to know – Kunal Mukherjeemongoose.Document
? – Kunal MukherjeePostDoc
theDocument
refers tomongoose.Document
right, have you destructured it on the top ? – Kunal Mukherjee