I have a jQuery AJAX function that sends some JSON data off to be stored in my postgresql database, I am using Node, Express and node-pg.
$.ajax({
type: 'POST',
data: newUser,
url: '/adduser',
dataType: 'JSON'
})
.done(function( response ) {
// Do something
})
.fail(function(errormsg) {
// Alert the sql error
alert(errormsg.responseText);
});
And in dealing with the insert in NodeJS on the client side I have
db.query('INSERT INTO userlist (' + keys + ') values ( ' + preppedString + ')', values, function(err, result) {
if (err){
var errorMessage = ("" + err);
res.send(errorMessage);
} else {
res.send(result);
}
});
Now, the problem is with the error message reporting. If I send the error message to the client side using
var errorMessage = ("" + err);
res.send(errorMessage);
It works perfectly - you get an alert saying stuff like "error: invalid input syntax for integer: "ds""
However, if i remove the string concatenation and just do
var errorMessage = err;
res.send(errorMessage);
It doesn't work at all - doesn't fire the .fail AJAX option (I think) and doesn't alert (definitely).
I'm not really too concerned with it - the first option with concatenation works fine, just wondering why it is behaving in that manner