i have string with ObjectId .
var comments = new Schema({
user_id: { type: Schema.Types.ObjectId, ref: 'users',required: [true,'No user id found']},
post: { type: Schema.Types.ObjectId, ref: 'posts',required: [true,'No post id found']}....
export let commentsModel: mongoose.Model<any> = mongoose.model("comments", comments);
How i user it:
let comment = new commentsModel;
str = 'Here my ObjectId code' //
comment.user_id = str;
comment.post = str;
comment.save();
When I create a "comment" model and assign a string user_id value or post I have an error when saving. I make console.log(comment)
all data is assigned to vars.
I try:
var str = '578df3efb618f5141202a196';
mongoose.mongo.BSONPure.ObjectID.fromHexString(str);//1
mongoose.mongo.Schema.ObjectId(str);//2
mongoose.Types.ObjectId(str);//3
- TypeError: Object function ObjectID(id) {
- TypeError: Cannot call method 'ObjectId' of undefined
- TypeError: Cannot read property 'ObjectId' of undefined
And of course I included the mongoose BEFORE ALL CALLS
import * as mongoose from 'mongoose';
nothing works.