[英]Mongoose relation one-to-many not work
I have a problem with mongoose schema relationship one-to-many: I have two documents company and agent, when I try to save the company document it cant be saved. 我的猫鼬模式关系一对多存在问题:我有两个文档company和agent,当我尝试保存公司文档时无法保存。 Note: the parent document is the company and the child is the agent: 注意:父文档是公司,子文档是代理商:
Company Schema : 公司架构 :
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Company Schema
*/
var CompanySchema = new Schema({
name: {
type: String,
default: '',
//required: 'Please fill Company name',
trim: true
},
created: {
type: Date,
default: Date.now
},
updated: {
type: Date,
default: Date.now
},
address: {
type: String,
default: '',
//required: 'Please fill Company address',
trim: true
},
locked: {
type: Boolean,
default: true,
},
deleted: {
type: Boolean,
default: false,
},
logo: {
type: String,
default: '',
},
email: {
type: String,
default: '',
//required: 'Please fill Company email',
},
tel: {
type: String,
default: '',
//required: 'Please fill Company tel',
},
fax: {
type: String,
default: '',
//required: 'Please fill Company fax',
},
type: {
type: String,
//required: 'Please fill Company type',
trim: true
},
description: {
type: String,
default: '',
trim: true
},
user: {
type: Schema.ObjectId,
ref: 'User'
},
agents: [{
type: Schema.ObjectId,
ref: 'Agent'
}]
});
mongoose.model('Company', CompanySchema);
Agent Schema 代理架构
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/**
* Agent Schema
*/
var AgentSchema = new Schema({
// Agent model fields
// ...
firstname: {
type: String,
default: ''
},
lastname: {
type: String,
default: ''
},
email: {
type: String,
default: ''
},
password: {
type: String,
default: ''
},
roles: {
type: Array,
},
created: {
type: Date,
default: Date.now
},
updated: {
type: Date,
default: Date.now
},
deleted: {
type: Boolean,
default: false
},
locked: {
type: Boolean,
default: false
},
company: {
type: Schema.ObjectId,
ref: 'Company'
}
});
var exports = module.exports = mongoose.model('Agent', AgentSchema);
API API
company.save(function(err){
if(err){
console.log('company err');
return res.status(400).send({
messages: errorHandler.getErrorMessage(err)
});
}else{
console.log('company save');
var agents = preapareAgents(req, company);
Agent.create(agents, function(err){
if(err){
return res.status(400).send({
messages: errorHandler.getErrorMessage(err)
});
}else{
res.jsonp({
company: company,
agents: agents
});
}
});
}
});
prepareAgent prepareAgent
var preapareAgents = function(req,company){
var admin = new Agent(req.body.admin);
admin.roles = ['admin', 'read','write'];
admin.company = company;
admin.locked = false;
var agents = req.body.agents;
for (var i = 0; i <= agents.length - 1; i++) {
agents[i].roles = [agents[i].role];
agents[i] = new Agent(agents[i]);
agents[i].company = company;
}
agents.push(admin);
return agents;
};
****Error: ****错误:
CastError: Cast to ObjectId failed for value "[object Object]" at path "agents" CastError:在路径“代理”中,对值“ [object Object]”的转换为ObjectId失败
In the prepareAgents method, the company of each agent must be the _id of the company and not the Object company. 在prepareAgents方法中,每个代理的公司必须是公司的_id,而不是对象公司。 Your code should look like: 您的代码应如下所示:
for (var i = 0; i <= agents.length - 1; i++) {
agents[i].roles = [agents[i].role];
agents[i] = new Agent(agents[i]);
agents[i].company = company._id; //here is the problem
}
Try it, maybe i'm wrong. 试试吧,也许我错了。
this is not the source of the problem, but when i delete the agents collection from the Company Model it works, this line cause the problem: 这不是问题的根源,但是当我从有效的公司模型中删除代理集合时,此行会导致问题:
agents: [{
type: Schema.ObjectId,
ref: 'Agent'
}]
我通过使用其他名称重命名代理集合来解决问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.