为什么我不能使用node-postgres从db中删除?

时间:2015-03-03 00:23:37

标签: node.js postgresql parameterized-query node-postgres

我需要对参数化查询做些什么特别的事情吗?

以下似乎成功了(我使用了承诺client.query看到结束),

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);
  });

因为我得到了输出:

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

但随后数据库的以下查询显示记录仍然存在

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

然后当我直接针对数据库(使用psql客户端)

时,以下查询成功
delete from friendship where user_1=3 and user_2_canonical='m'

我的query函数(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;
}

1 个答案:

答案 0 :(得分:0)

试试这个

var cancelInviteQuery = 'delete from friendship where user_1 IN ($1) and user_2_canonical IN ($2)';