skip to Main Content

app.post(‘/contactForm’,function(req,res){

      Contact.create({
        name: req.body.name,
        phone: req.body.phone
    },  function(err, newContact){
        if(err){
            console.log(err, 'error in connectiong');
            return;
        }
        console.log("*******", newContact);
        return   res.redirect('back');
    })

  

})

/home/ganesh/Web Dev/CN Web Dev/HTML/basics/Nodejs Workspace/contact_list/node_modules/mongoose/lib/model.js:2771
throw new MongooseError(‘Model.create() no longer accepts a callback’);

2

Answers


  1. Chosen as BEST ANSWER

    it should be like

    app.post('/contactForm',function(req,res){

     Contact.create({
            name: req.body.name,
            phone: req.body.phone
        }).then( (newContact) => {
            console.log("*******", newContact);
            return   res.redirect('back');
        }).catch((err) => {
            console.error(err);
        })
    

    })


  2. https://mongoosejs.com/docs/migrating_to_7.html#dropped-callback-support

    exports.createUser = async (req, res) => {
      const { firstName, phone } = req.body;
    
      try {
        const user = await User.create({
          firstName,
          phone,
        });
        return res.redirect("back");
      } catch (err) {
        return console.log(err, "error in connectiong");
      }
    };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search