app.post ('/create_contact',function(req,res)
{
contact.create({
name :req.body.name,
phone:req.body.phone
},function(err,newContact)
{
if(err){console.log('error in creating a contact!');
return;}
console.log('********',newContact);
return res.redirect('back');
});
});
错误:
throw new MongooseError('Model.create() no longer accepts a callback');
^
MongooseError: Model.create() no longer accepts a callback
我试过这段代码但再次显示错误
你可以用
async await
解决
app.post('/create_contact', async function (req, res) {
try {
const newContact = await contact.create(req.body);
console.log('********', newContact);
return res.redirect('back');
} catch (err) {
res.send('error in creating a contact!');
}
});