Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.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
Javascript 如何修复节点中的mongoose数组_Javascript_Node.js_Mongoose_Mongoose Schema - Fatal编程技术网

Javascript 如何修复节点中的mongoose数组

Javascript 如何修复节点中的mongoose数组,javascript,node.js,mongoose,mongoose-schema,Javascript,Node.js,Mongoose,Mongoose Schema,我正在尝试将对象推入我的mongodb。 在我的console.log中推送时,我能够查看所述数据。但他们根本没有被推入数据库 下面是我的app.js代码 const userSchema = new mongoose.Schema({ email : String, password: String, selectedPet : [String] }); userSchema.plugin(passportLocalMongoose); const User =

我正在尝试将对象推入我的mongodb。 在我的console.log中推送时,我能够查看所述数据。但他们根本没有被推入数据库

下面是我的app.js代码

const userSchema = new mongoose.Schema({
    email : String,
    password: String,
    selectedPet : [String]
});



userSchema.plugin(passportLocalMongoose);

const User = new mongoose.model("User", userSchema);

app.post("/prototype",(req, res) =>{
    var animalSelected = [];
     animalSelected.push = req.body.selectedPet;

  console.log(req.user.id, animalSelected);
  User.findById(req.user.id, function(err, foundUser){
    if (err){
        console.log(err);
    } else {
        if (foundUser){
            foundUser.selectedPet = animalSelected;
            foundUser.save(function(){
                res.redirect("/proto")
            });
        }
    }
  });
  });

我不确定您的代码是否正确,但您应该尝试仅使用方法和
$push
运算符来推送数组中的元素。我只是一个菜鸟,你能不能帮我查一下,你的意见会很感激的。。