Reputation: 11
I 'm trying to bind my Model with a mongoose schema using Typescript. Everything looks good except when I use ObjectId inside mongoose.Types.DocumentArray<>.
export interface TestModel extends mongoose.Document {
name: string;
sort: mongoose.Types.DocumentArray<mongoose.Schema.Types.ObjectId>;
}
const TestSchema = new mongoose.Schema({
name: String,
sort: [mongoose.Schema.Types.ObjectId]
});
export default mongoose.model<TestModel>('Test', TestSchema);
Following is the error I see
Type 'ObjectId' does not satisfy the constraint 'MongooseDocument'.
Type 'ObjectId' is missing the following properties from type 'MongooseDocument': $isDefault, $session, depopulate, equals, and 24 more.ts(2344)
If I use
sort: [mongoose.Schema.Types.ObjectId];
instead of
sort: mongoose.Types.DocumentArray<mongoose.Schema.Types.ObjectId>;
Everything looks good on schema side but I see mongoose type errors with methods like .pull() when doing operations on the modal results (TS is not picking it up as mongoose array type.)
Scratching my head with this for hours, any help will be really appreciated.
Upvotes: 0
Views: 2763
Reputation: 11
Found the problem. I'm stupid, it's a silly mistake I was using wrong Type in type definition. Should be using mongoose.Types.Array
instead of mongoose.Types.DocumentArray
Upvotes: 1