通过使用这个函数,我无法使用mongoose填充我的mongodb



这是代码......

app.post('/create-contact',function(req,res){
Contact.create().then(function(newContact){
({name:req.body.name, 
phone:req.body.phone});
console.log('*******************',newContact);
return res.redirect('back');
}).catch(function(err,newContact){
if(err){
console.log("Error in creating the contact!");
return;
}
});

that newContact is get null…我在req。body。name和req.body.phone中获得值,但它没有存储在newContact中,也没有在DB

中更新

您使用圆括号而不是花括号来定义newContact对象的属性。将括号替换为花括号并检查。这样的:

Contact.create({
name: req.body.name,
phone: req.body.phone
}).then(function(newContact){
console.log('*************',newContact);
return res.redirect('back');
}).catch(function(err){
console.log("Error in creating 
the contact!", err);
return res.status(500).send("Error creating
contact");
});

此外,确保Contact模型具有正确的模式并正确连接到您的MongoDB数据库。