How to use save on Node js?

Asked

Viewed 79 times

0

I’m with a study project using Node JS with Mongodb and I’m having difficulty saving a record in the database, I’m using the method this way.

function postRestaurant (req, res) {
    var restaurants = new Restaurants(req.body);
    restaurants
    .save
    ((err, restStored) => {
        if(err){
            res.status(500).send({message: 'Error do servidor'});
        }else{
            if(!restStored){
                res.status(404).send({message: 'No existe restaurant'});
            }else{
                res.status(200).send({restaurants: restStored});
            }
        }
    });

}

When I submit the method it’s falling into error 500, where am I wrong?

  • Give a console.log(err), to see the reason for the error.

  • Isn’t it missing to pass the object that will be saved? I’m only seeing the callback function, without the record itself.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.