Javascript 如何修复节点中的mongoose数组
我正在尝试将对象推入我的mongodb。 在我的console.log中推送时,我能够查看所述数据。但他们根本没有被推入数据库 下面是我的app.js代码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 =
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
运算符来推送数组中的元素。我只是一个菜鸟,你能不能帮我查一下,你的意见会很感激的。。