【发布时间】:2016-11-21 15:06:12
【问题描述】:
我有一个带有 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);
我是如何使用它的:
let comment = new commentsModel;
str = 'Here my ObjectId code' //
comment.user_id = str;
comment.post = str;
comment.save();
当我创建“评论”模型并分配字符串 user_id 值或发布时,我在保存时出错。我把console.log(comment) 的所有数据都分配给了vars。
我试试:
var str = '578df3efb618f5141202a196';
mongoose.mongo.BSONPure.ObjectID.fromHexString(str);//1
mongoose.mongo.Schema.ObjectId(str);//2
mongoose.Types.ObjectId(str);//3
- TypeError: 对象函数 ObjectID(id) {
- TypeError:无法调用未定义的方法“ObjectId”
- TypeError:无法读取未定义的属性“ObjectId”
当然我也包括了猫鼬在所有呼叫之前
import * as mongoose from 'mongoose';
没有任何作用。
【问题讨论】:
-
该问题中没有任何方法有帮助(
-
然后开始显示一些代码/错误消息,这些消息应该从一开始就包含在您的问题中。当您不显示失败的原因(您的代码)以及失败的原因(错误)时,我们无法为您提供帮助
-
我展示了字符串的代码和示例