I am trying to do an insert or update in a postgres database using node.js with the pg extension (version 0.5.4).
So far I have this code: (...)
client.query({ text: "update users set is_active = 0, ip = $1 where id=$2", values: [ip,id] }, function(u_err, u_result){ debug(socket_id,"update query result: ",u_result); debug(socket_id,"update query error: ",u_err); date_now = new Date(); var month = date_now.getMonth() + 1; if(!u_err){ client.query({ text: 'insert into users (id,first_name,last_name,is_active,ip,date_joined) values' + '($1,$2,$3,$4,$5,$6)', values: [ result.id, result.first_name, result.last_name, 1, ip, date_now.getFullYear() + "-" + month + "-" + date_now.getDate() + " " + date_now.getHours() + ":" + date_now.getMinutes() + ":" + date_now.getSeconds() ] }, function(i_err, i_result){ debug(socket_id,"insert query result: ",i_result); debug(socket_id,"insert query error: ",i_err); }); } });
The problem is that although both requests work, the problem always runs as instead of running the insert function if the update does not work.
The debugging functions in the code output something like:
UPDATE
Object { type="update query result: ", debug_value={...}} home (linha 56) Object { type="update query error: ", debug_value=null} home (linha 56) Object { type="insert query result: "} home (linha 56) Object { type="insert query error: ", debug_value={...}}
Embed
Object { type="update query result: ", debug_value={...}} home (linha 56) Object { type="update query error: ", debug_value=null} home (linha 56) Object { type="insert query result: ", debug_value={...}} home (linha 56) Object { type="insert query error: ", debug_value=null}
** EDIT **
ANSWER FROM the developer node-postgres:
You can get the number of rows affected by the insert and Refresh. It is not fully implemented in its own bindings, but does work in a clean version of javascript. I will be working on this as part of next week or two. In the meantime, use a clean version of javascript and look here:
https://github.com/brianc/node-postgres/blob/master/test/integration/client/result-metadata-tests.js
** END EDIT **
Can anyone help?