Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 插入mongoose数组元素_Node.js_Mongodb_Post_Mongoose_Mongoose Schema - Fatal编程技术网

Node.js 插入mongoose数组元素

Node.js 插入mongoose数组元素,node.js,mongodb,post,mongoose,mongoose-schema,Node.js,Mongodb,Post,Mongoose,Mongoose Schema,我试图使用mongoose模式将数据插入mongoDB,该模式由answer1作为字符串数组组成,但我不知道当我们收到用户的响应时是否必须执行此操作。模式如下: const answerschema = new mongoose.Schema({ answer1: [String], }); const User = new mongoose.model("User", answerschema); app.post("/template", fu

我试图使用mongoose模式将数据插入mongoDB,该模式由answer1作为字符串数组组成,但我不知道当我们收到用户的响应时是否必须执行此操作。模式如下:

const answerschema = new mongoose.Schema({
  answer1: [String],
});

const User = new mongoose.model("User", answerschema);
app.post("/template", function (req, res) {
 
  const user = new User({
    user.answer1.push(req.body.answer);
    user.save();
    res.redirect("/template");

  });

});
我还创建了用户提交答案时页面的post方法。现在,我尝试将此响应存储到answer1数组中,如下所示:

const answerschema = new mongoose.Schema({
  answer1: [String],
});

const User = new mongoose.model("User", answerschema);
app.post("/template", function (req, res) {
 
  const user = new User({
    user.answer1.push(req.body.answer);
    user.save();
    res.redirect("/template");

  });

});
但我在users.answer1.push上得到了错误作为意外标记“.”。 如何删除此错误