In express js, I have the following code which sends a response over to Backbone:
if (!user) {
req.session.messages = [info.message];
return res.send(400, howCanIsendTheErrorMessageAlso);
}
How can I send the error message also, together with the error code?
How can Backbone receive it?
Any ideas?
In backbone, I have the following code:
loginModel.save({
username : obj.elEmail.val(),
password : obj.elPassword.val(),
admin : false
}, {
success: function (e) {
console.log('success');
},
error: function (e) {
console.log(e);
}
});
Any ideas?
You send it from express with:
res.send(500, { error: "hi, there was an error" });
In Backbone the parameters of your error callback are: model, xhr, options
So you need to extract your error message fron the xhr object in the error callback like this:
obj.save(
{
data:"something"
},
{
success: function(model, response, options){
console.log("worked");
},
error: function(model, xhr, options){
console.log("error", xhr.responseText);
}
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With