Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending custom error message from Express JS over to Backbone

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?

like image 855
Dany D Avatar asked Feb 15 '23 05:02

Dany D


1 Answers

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);
    }
   }
);
like image 132
homtg Avatar answered Feb 17 '23 01:02

homtg