我有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();
当我创建一个" comments "模型并分配一个字符串user_id值或post时,我在保存时出错.我把所有数据都分配给VAR.
我试着:
var str = '578df3efb618f5141202a196';
mongoose.mongo.BSONPure.ObjectID.fromHexString(str);//1
mongoose.mongo.Schema.ObjectId(str);//2
mongoose.Types.ObjectId(str);//3
- 类型错误:对象函数ObjectID(id){
- TypeError:无法调用未定义的方法"ObjectId"
- TypeError:无法读取未定义的属性"ObjectId"
当然,我还包括mongoose BEFORE ALL CALLS
import * as mongoose from 'mongoose';
什么都不管用.