我在我的 Express/Mongoose/Mongo 应用程序中发布嵌套模型时遇到问题。当我尝试同时发布单个容器模型和多个内部模型时,仅外部模型(订单)显示在数据库中。
架构:
var itemSchema = new Schema({
order: ObjectId,
type: String // ex: Sandwich
});
var orderSchema = new Schema({
name: String,
items: [Item.itemSchema]
});
接口:
var Order = require('../models/order.js');
var Item = require('../models/item.js');
exports.createOrder = function(req, res) {
new Order({name: req.body.name}, function(err, order){
order.items.forEach(function (err, item){
new Item({order: order._id, type: item.type}).save;
});
}).save();
};
卷发:
curl -i -X POST -H 'Content-Type: application/json' -d '{"name": "John Smith","items": [{"type": "sandwich"},{"type": "sub"}]}' http://localhost:3000/order