skip to Main Content
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');
  });
});

Errors:

throw new MongooseError('Model.create() no longer accepts a callback');
          ^
MongooseError: Model.create() no longer accepts a callback

I tried this code but again showing error

2

Answers


  1. Mongoose has dropped support for callbacks to some of it’s methods (The create method is one of them). You can check out this link to see more about it. And instead of using callbacks, using async/await syntax and wrap it with a try/catch block to handle errors that might happen on creation of a new document.

    Check this this duplicate question. Your question will be flagged.

    Login or Signup to reply.
  2. You can solve it with 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!');
      }
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search