I am learning Backend development using node.js and mongodb.
Mongoose has a method Model.save(function(err,model))
what status code must be returned when for some reason the document could not be saved.
In terms of code:
Model.save(function(err,model){
if(err)
res.status(5xx).send(err);
if(!model) {
//executed when unable to save document IMHO correct me I am wrong
res.status(xxx).send("Unable to save the model in database");
}
});
Well, this is a tricky question but I have to tell you 422 is not a valid state.
What I imagine is you are trying to say there was an error, it might be a network problem, service not ready (Mongo down, unresponsive) or similar. There is no 4xx code to represent that, if you want to use any 4xx it might not be representing the truth.
The usual response is 503 because it's unavailable which is what I imagine you want to communicate.