**model schema**
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var item = new Schema({
name:{type: String,
required: true},
price:{type: String}
})
var object=new Schema({
name:{type: String,
required: true},
items: [item]
});
api.js,将数据发布到服务器
router.post('/objectss', function(req, res){
var object= new Object(); **
如何保存数组中的对象数组
**
object.items.push({firstName: req.body.fname, lastName: req.body.lname});
object.name = req.body.name;
object.save(function (err) {
if(err) throw err;
res.json({success: true, message:'allahuakbar'});
});
//已保存!
});
angularjs控制器
$ scope.addProduct = function(){
$scope.items.push({
fname: $scope.item.fname,
lname: $scope.item.lname
});
$scope.item = {};
}
$scope.submitx = function(inv, item){
console.log(inv);
var object = {
name:inv.name,
fname:items.fname,
totalValue: 0
}
PostBlog.createInvoice(objects).then(function(data){
console.log(data);
});
$scope.objects= {};
}
请看这个代码并帮助我!挣扎了几个星期。有一个对象的数组,如[“名称”:胡萝卜,“价格”:24,“”:等等]
答案 0 :(得分:0)
那些卡住的人会参考这个而不是使用方法
router.post('/invoices', function(req, res){ //by making route in from
var invoice = new Invoice();
for (var i = 0; i < req.body.length; i++) {
invoice.items.push({name:req.body.fname[i].fname});
}
invoice.name = req.body.name;
//invoice.items.insertMany(items, function(error, next) {
invoice.save(function (err) {
if(err) throw err;
res.json({success: true, message:'allahuakbar'});
});
// saved!
});