I have the following code in ajax request:
403: function(jqXHR) {
var error = jqXHR.responseText;
console.log(error);
}
and when this error happens i get next message in console:
HttpError: Wrong password<br> at
c:\Users\...path...js:18:29<br> at
c:\Users\...path...\async.js:52:16<br> at
Immediate._onImmediate
(c:\Users\...path...
ode_modules\async\lib\async.js:1201:34)<br>
at processImmediate [as _immediateCallback]
(timers.js:383:17)
When i try to parse this error and get the error.message in such way:
var error = JSON.parse(jqXHR.responseText);
console.log(error.message);
I have the following message in console:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
.complete()
m.Callbacks/j()
m.Callbacks/k.fireWith()
x()
.send/b()
here is the server-side code:
schema.statics.authorize = function(username, password, callback) {
var User = this;
async.waterfall([
function(callback) {
User.findOne({username: username}, callback);
},
function(user, callback) {
if (user) {
if (user.checkPassword(password)) {
callback(null, user);
} else {
callback(new AuthError("Wrong password"));
}
} else {
var user = new User({username: username, password: password});
user.save(function(err) {
if (err) return callback(err);
callback(null, user);
});
}
}
], callback);
};
exports.User = mongoose.model('User', schema);
function AuthError(message) {
Error.captureStackTrace(this, AuthError);
this.message = message;
}
util.inherits(AuthError, Error);
AuthError.prototype.name = 'AuthError';
exports.AuthError = AuthError;
How can i display an error.message to console.log?