Argument of type 'import("mongoose").Types.ObjectId' is not assignable to parameter of type 'import("mongoose").Schema.Types.ObjectId'. Type 'ObjectId' is missing the following properties from type 'ObjectId': auto, defaultOptions, OptionsConstructor, cast, and 20 more.ts(2345)
this error is showing when i convert an string objectId to to mongoose Objectid;
const questId = new mongoose.Types.ObjectId(questionId as string )
const response = await this.__questionRepository.createNewAnswer(
answer,
questId,// here showing the above typescript error
userId,
userType
);
//this is how i defined the questionId in Schema
questionId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Question',
required: true
}
Argument of type 'import("mongoose").Types.ObjectId' is not assignable to parameter of type 'import("mongoose").Schema.Types.ObjectId'. Type 'ObjectId' is missing the following properties from type 'ObjectId': auto, defaultOptions, OptionsConstructor, cast, and 20 more.ts(2345)
this error is showing when i convert an string objectId to to mongoose Objectid;
const questId = new mongoose.Types.ObjectId(questionId as string )
const response = await this.__questionRepository.createNewAnswer(
answer,
questId,// here showing the above typescript error
userId,
userType
);
//this is how i defined the questionId in Schema
questionId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Question',
required: true
}
Share
Improve this question
edited Jan 20 at 16:27
Aswanth
asked Jan 20 at 0:47
AswanthAswanth
12 bronze badges
2
- Hello, this is because the questionId might be getting null/undefined so try to check whether it is correct or not. – user28234598 Commented Jan 20 at 12:49
- const questId = new mongoose.Types.ObjectId(questionId as string) as unknown as mongoose.Schema.Types.ObjectId; //in schema questionId: { type: mongoose.Types.ObjectId, ref: 'Question', required: true } i solved this error by making change like this – Aswanth Commented Jan 20 at 16:19
1 Answer
Reset to default 0//atlast i solved this issue by making these changes
const questId = new mongoose.Types.ObjectId(questionId as string) as unknown as mongoose.Schema.Types.ObjectId;
//in schema
questionId: {
type: mongoose.Types.ObjectId,
ref: 'Question',
required: true
}