我是mongoose和节点的新手,需要你帮忙.....我有我的架构
var creditSchema = new Schema({
mount: { type: Number, default: 0 },
period: Number,
allDayPay: [{periods: String, dayPay: String, mountPay: String}],
created: { type: Date, default: Date.now },
active: { type: Boolean, default: true },
});
我有一个表单,这个表单有一个类型文本,根据句点数生成,例如,如果把10个句点jquery生成10个文本字段,这段代码:
$('#calculate').click(function(){
var period = $("#period").val();// This can be 10 or 15 or 30.....
for (i = 0; i < period; i++){
var textPeriod = '
<input type="text" id="periods'+[i+1]+'" name="periods'+[i+1]+'" value="Pago-'+[i+1]+'" />
<input type="text" id="dayPay'+[i+1]+'" name="dayPay'+[i+1]+'" value="'+moment(allPay._d).format('LL')+'" />
<input type="text" id="mountPay'+[i+1]+'" name="mountPay'+[i+1]+'" value="'+mountPay+'" />';
$('#listAll').append(textPeriod);
}
});
这个表格很好!没有问题。
我也有尝试保存数据的路由器帖子
router.post('/saveCredit', function(req, res){
var credits = new Credits();
credits.mount = req.body.mount;
credits.period = req.body.period;
credits.allDayPay = [];
for(i=0; i<=req.body.period; i++){
credits.allDayPay = [{periods: req.body.periods[i],(dayPay: req.body.dayPay[i], mountPay: req.body.mountPay[i])}];
};
credits.save(function(err){
if(err){
res.send(err);
};
res.redirect('/');
});
});
我尝试了这种方法和博客中没有成功的其他方法,问题是我如何将这些数据(句点,dayPay,mountPay)保存在名为'allDayPay'(数组)的字段模式中,如果我发布方法? ? 这应保存为以下格式。
"allDayPay": [
{
"periods": 1,
"dayPay": 12/09/15;
"mountPay": 150
},
{
"periods": 2,
"dayPay": 12/10/15;
"mountPay": 150
},
{
"periods": 3,
"dayPay": 12/11/15;
"mountPay": 150
}
....
....
....
....
....
...... everything that can be save depending of number of periods specified in the form
]
请向我解释如何保存这些数据,谢谢!
答案 0 :(得分:0)
试试这个可行:
for(i=0; i<=req.body.period; i++){
var temp = '{periods:'+ req.body.periods[i]+',(dayPay: '+req.body.dayPay[i]+', mountPay:'+ req.body.mountPay[i]+')}';
credits.allDayPay.push(temp);
};