【发布时间】:2014-07-19 05:44:01
【问题描述】:
我正在开发一个基于 Mean Stack 的 Web 应用程序,但我很关心添加和编辑操作,特别是对于字段“订单”的子文档,控制台告诉我“引用”未定义就行“order.reference”:req.body.order.reference, ,我不知道如何处理子文档。当我添加或修改任何“订单”字段时,我得到了一个错误,但是当我毫无例外地添加所有字段时它可以工作。这是我的猫鼬图:
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var ContactSchema = new Schema({
name: {type: String},
order: {
reference : {type : String},
adresse : {type : String} ,
product : {type : String}
}
});
var ContactModel = mongoose.model('Contact', ContactSchema);
mongoose.connect('mongodb://localhost/contact');
exports.add = function(req, res) {
var contact = req.body;
contact = new ContactModel({
name: req.body.name,
"order.reference" : req.body.order.reference,
"order.adresse" : req.body.order.adresse,
"order.product" : req.body.order.product
});
contact.save(function (err) {
if (!err) {
res.json(true);
} else {
console.log(err);
res.json(false);
}
});
return res.jsonp(req.body);
};
exports.edit = function (req, res) {
var id = req.params.id;
if (id) {
ContactModel.findById(id, { upsert: true }, function (err, contact) {
contact.name = req.body.name,
contact.order.reference = req.body.order.reference,
contact.order.adresse = req.body.order.adresse ,
contact.order.product = req.body.order.product
contact.save(function (err) {
if (!err) {
res.json(true);
} else {
res.json(false);
console.log(err);
}
});
});
}
};
谢谢你的时间,我可以提供角度代码和html
【问题讨论】:
标签: express mongoose mean-stack