我有一个称为test的数组,用于存储有关特定测试的信息。我正在使用评论功能,以便用户可以评论测试。目前,用户只能发表一个评论,因为第二个评论将覆盖第一个评论。我现在想在每次输入注释时在“注释”数组中添加一个对象。
var test = [{
id: 0,test_id: "Password test",pass: 1,fail: 5,time: 0.03,pass_fail: 20,comments : [
{comment : "",commentuser : "" },]
}
我使用push()函数来做到这一点:
app.post('/test/:id/comment',urlencodedParser,function(req,res) {
console.log("User Name : " + req.body.name);
console.log("User Comment: " + req.body.comment);
if (req.body.name && req.body.comment) {
console.log('Your comment was posted !');
res.sendFile(__dirname + '/HTML/comment-success.html');
//test[req.params.id].comment = req.body.comment ;
//test[req.params.id].commentuser = req.body.name ;
var input = test[req.params.id].comments.comment;
input.push(req.body.comment);
但是我得到以下错误: TypeError:无法读取未定义的属性“ push” 。 在此先感谢您的帮助。 干杯