Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is there something special i need to do with a parameterized query?

the following seems to succeed (i'm using a promise-ified client.query see end),

console.log('cancel for', data);
var cancelInviteQuery = 'delete from friendship where user_1=$1 and user_2_canonical=$2';
return query(cancelInviteQuery, [data.id, data.friend])
  .then(function (results) {
    console.log('ran delete frienship', results);
  })
  .catch(function (error) {
    console.error('failed to drop friendship', error);
  });

because i get the output:

cancel for {"id":3,"friend":"m"}
ran delete frienship []

but then the following query of the database shows the record still exists

select * from friendship;
 id | user_1 | user_2 | user_2_canonical | confirmed | ignored 
----+--------+--------+------------------+-----------+---------
  8 |      3 |      9 | m                | f         | f

and then the following query succeeds when I make it directly against the database (using psql client)

delete from friendship where user_1=3 and user_2_canonical='m'

my query function (a wrapper for node-postgres's client.query):

function query(sql, params) {
  var promise = new RSVP.Promise(function (resolve, reject) {
    pg.connect(CONNECTIONSTRING, function (error, client, done) {
      if (error) { reject(error); return; }

      client.query(sql, params, function (error, result) {
        done(); // Greedy close.
        if (error) {
          reject(error);
          return;
        }
        else if (result.hasOwnProperty('rows')) {
          resolve(result.rows);
        } else { resolve(result.rows); }
      });
    });
  });
  return promise;
}
share|improve this question
    
Something is wrong with your data variable. In the console.log it is displaying "friend":"m" but you are expecting it to be "friend":"mike". –  victorkohl Mar 3 at 0:29
    
sorry @victorkohl it was a sloppy paste. –  Michael Mar 3 at 0:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.