我有两个Schema
对象:
联系人.js:
/**
* Contact Schema
*/
var ContactSchema = new Schema({
name: String,
role: String,
phone: String,
email: String,
primary: Boolean
}, {timestamps: {createdAt: 'created', updatedAt: 'updated'}, _id: true, id: true});
客户端.js:
/**
* Client Schema
*/
var ClientSchema = new Schema({
name: {
type: String,
required: true,
trim: true
},
comments: {
type: String,
trim: true
},
creator: {
type: Schema.ObjectId,
ref: 'User'
},
contacts: [ContactSchema],
address: String,
}, {timestamps: {createdAt: 'created', updatedAt: 'updated'}});
唉,当我保存Client
对象时,没有 _id 分配给已保存的Contact
.
但是当我使用这个模式时:
客户端.js:
/**
* Client Schema
*/
var ClientSchema = new Schema({
name: {
type: String,
required: true,
trim: true
},
comments: {
type: String,
trim: true
},
creator: {
type: Schema.ObjectId,
ref: 'User'
},
contacts: [{
name: String,
role: String,
phone: String,
email: String,
primary: Boolean
}],
address: String,
}, {timestamps: {createdAt: 'created', updatedAt: 'updated'}});
联系人使用自动生成的 _id 保存。
我保存客户的方式非常简单:
var client = new Client(req.body);
client.creator = req.user;
client.save(function (err) {
if (err) {
console.log(err);
return res.status(500).json({
error: 'Cannot save the client'
});
}
res.json(client);
});
req.body 的内容是:
{
name: 'A name for the client',
contacts: [ {
name: 'A name for the contact',
email: 'noy@test.com',
role: 'UFO'
}]
}
我错过了什么?