【发布时间】:2018-12-11 21:07:53
【问题描述】:
我已经制作了我的模型,它有这个架构:
const CompanySchema = new Schema({
companyName: {
type: String,
required: [true,'the name of the companies working on the game are missing.']
},
companyAge: {
type: Number,
required: [true,'the age of the company is missing.']
},
companyDeveloper:[{
type: Schema.Types.ObjectId,
ref: "developer"
}]
});
我正在尝试将一个元素推入 companyDeveloper 数组,如下所示:
addDev(req,res,next){
const companyId = req.params.id;
const companyDeveloper = ObjectId.fromString(req.body.companyDeveloper);
Company.findById({_id: companyId})
.then((company) => company.companyDeveloper.push({companyDeveloper}))
.then(company => res.send(company))
.catch(next);
}
但我不断收到此错误: "error": "ObjectId 未定义"。
在我尝试投射它之前,我收到了这个错误 转换为 ObjectId 的值失败
我怎样才能让这个功能发挥作用?
打印屏幕
【问题讨论】:
标签: mongodb mongoose mongodb4.0