Javascript 使用额外字段引用Mongoose中的另一个架构
用户可以有多个作业。当我向用户添加作业时,我会这样做: 如果我有:Javascript 使用额外字段引用Mongoose中的另一个架构,javascript,node.js,express,mongoose,Javascript,Node.js,Express,Mongoose,用户可以有多个作业。当我向用户添加作业时,我会这样做: 如果我有: var UserSchema = new Schema({ job : [{ type : mongoose.Schema.Types.ObjectId, experience : String, grade : String, ref: 'Company'}] }); 及 我有一个错误: function addJob(req, res, next) { var userId = r
var UserSchema = new Schema({
job : [{
type : mongoose.Schema.Types.ObjectId,
experience : String,
grade : String,
ref: 'Company'}]
});
及
我有一个错误:
function addJob(req, res, next) {
var userId = req.user._id;
var job = req.body.job;
return User.findById(userId).exec()
.then(user => {
user.job.push(job);
return user.save()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
});
}
但如果我这样做:
Unhandled rejection CastError: Cast to ObjectId failed for value "[object Object]" at path "job"
与
我试着:
var UserSchema = new Schema({
job : [{
_company : {
type : mongoose.Schema.Types.ObjectId,
ref: 'Company'
},
experience : String,
grade : String
}]
});
我从服务器收到500(内部服务器错误)错误
但是
//req.body.job={公司:{类型:公司。{id},经验:“bla-bla”,等级:“smth”}
函数addJob(请求、恢复、下一步){
var userId=req.user.\u id;
var job=req.body.job;
返回User.findById(userId.exec())
。然后(用户=>{
user.job.push(job.u company);//字段“type”由Mongoose保留,然后
//req.body.job == {_company:{type : company._id}, experience : "bla bla", grade : "smth"}
function addJob(req, res, next) {
var userId = req.user._id;
var job = req.body.job;
return User.findById(userId).exec()
.then(user => {
user.job.push(job._company); //<-----------------
return user.save()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
});
}
正在使用ObjectId类型描述实体,引用公司(此字段与.populate一起使用)。不幸的是,由于它作为ObjectId保存在MongoDB中,其他字段将被忽略
然后,您可以选择在其中一个字段中引用该公司,并能够使用如下JobSchema检索额外的信息位:
{
type : mongoose.Schema.Types.ObjectId,
experience : String,
grade : String,
ref: 'Company'
}
您将能够在不丢失“经验”和“等级”信息的情况下填充工作的_company字段
在您的版本之后:
对于给定的JobSchema:
{
_company : {type: mongoose.Schema.Types.ObjectId, ref: 'Company'}
experience : String,
grade : String
}
你的工作实例应该看起来像{你的公司:ObjectId(“…”),经验:“blabla”,等级:“smth”}。然后你的req.body.job应该是{你的公司:company.{id,经验:“bla bla”,等级:“smth”}
在您的第一个addJob中,您有一个500错误,因为您要求MongoDB将一个看起来像{type:ObjectId(“…”)}的文档转换为ObjectId
第二个addJob不会触发错误,因为您正在上载一个看起来像{type:ObjectId(“…”)}的文档(顺便说一句,它丢失了grade和experience字段),并且不需要任何模式字段,所以mongoose忽略了您的字段类型,并将空对象上载到集合中
tl;dr:
应该是:
{
_company : {type: mongoose.Schema.Types.ObjectId, ref: 'Company'}
experience : String,
grade : String
}
是的,我是这样想的。所以我想没有其他解决方案了,对吗?@Emidomh我不认为有其他解决方案,对不起:/@Emidomh在您编辑后编辑:>第一个addJob是正确的,但是请求的正文。_company不是:它应该是company。_id而不是{type:company。_id}@Emidomh您还应该尝试User.findOneAndUpdate({{u id:userId},{$push:{job:job}}).exec(/*....*/)
//req.body.job == {_company:{type : company._id}, experience : "bla bla", grade : "smth"}
function addJob(req, res, next) {
var userId = req.user._id;
var job = req.body.job;
return User.findById(userId).exec()
.then(user => {
user.job.push(job._company); //<-----------------
return user.save()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
});
}
{
type : mongoose.Schema.Types.ObjectId,
experience : String,
grade : String,
ref: 'Company'
}
{
_company : {type: mongoose.Schema.Types.ObjectId, ref: 'Company'}
experience : String,
grade : String
}
{
_company : {type: mongoose.Schema.Types.ObjectId, ref: 'Company'}
experience : String,
grade : String
}
// req.body.job == {_company: company._id, experience : "bla bla", grade : "smth"}
function addJob(req, res, next) {
var userId = req.user._id,
job = req.body.job;
return User.findById(userId)
.exec()
.then(
user => {
user.job.push(job);
return user.save()
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
);
}