node.js - 为什么我不能使用 node-postgres 从数据库中删除?

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

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

以下似乎成功了(我正在使用 promise-ified 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 的 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;
}

最佳答案

试试这个

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

关于node.js - 为什么我不能使用 node-postgres 从数据库中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28821899/

相关文章:

Node.js 和 mongoDB 服务并发请求

node.js - nodejs - 无法让 paypal-ipn 工作。 Paypal 一直返回 "INVALID"

mysql - 是否可以为 MySQL 和 PostgreSQL 编写 SQL 脚本?

python - pymysql:MySQL 参数化 LIKE 查询

javascript - Twit API 回复并没有真正回复

sql - 在 PostgreSQL 中 : FROM generate_subscripts($1, 1) g(i) 中的 g(i) 是什么?

java - 使用 JPA 时如何解决 Postgres 中的 "No results were returned by the query"异常

asp-classic - ADODB.Recordset 错误 '800a0bb9' 参数类型错误、超出可接受范围或相互冲突

python - :TypeError: %d format: a number is required, 不是 str:在 python pandas 中给出了偶数

javascript - Ajax成功不使用node js执行