将自定义错误消息从 Express JS 发送到 Backbone

Dan*_*y D 4 javascript node.js express backbone.js

在 express js 中,我有以下代码向 Backbone 发送响应:

if (!user) {
    req.session.messages = [info.message];
    return res.send(400, howCanIsendTheErrorMessageAlso);
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能同时发送错误消息和错误代码?

Backbone 如何接收它?

有任何想法吗?

在主干中,我有以下代码:

loginModel.save({
    username : obj.elEmail.val(),
    password : obj.elPassword.val(),
    admin : false
}, {
    success: function (e) {
        console.log('success');
    }, 
    error: function (e) {
        console.log(e);
    }   
});
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

hom*_*mtg 6

您从快递发送它:

res.send(500, { error: "hi, there was an error" });
Run Code Online (Sandbox Code Playgroud)

在 Backbone 中,错误回调的参数是:model、xhr、options

因此,您需要在错误回调中从 xhr 对象中提取错误消息,如下所示:

obj.save(
   {
    data:"something"
   },
   {
    success: function(model, response, options){
     console.log("worked");
    },
    error: function(model, xhr, options){
     console.log("error", xhr.responseText);
    }
   }
);
Run Code Online (Sandbox Code Playgroud)