【问题标题】:node-postgres: done() - undefined is not a functionnode-postgres: done() - undefined 不是函数
【发布时间】:2015-10-27 05:31:57
【问题描述】:

我在使用 node-postgres 的查询功能时遇到问题。

选择、插入和删除都成功执行了数据库查询,但是插入和删除在节点服务器上触发了这个错误:

/vagrant/catalog/database/db.js:11
                done();
                ^
TypeError: undefined is not a function
    at null.callback (/vagrant/catalog/database/db.js:11:17)
    at p.handleReadyForQuery (/vagrant/catalog/node_modules/pg/lib/query.js:78:10)
    at null.<anonymous> (/vagrant/catalog/node_modules/pg/lib/client.js:111:24)
    at emit (events.js:117:20)
    at Socket.<anonymous> (/vagrant/catalog/node_modules/pg/lib/connection.js:47:12)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:765:14)
    at Socket.emit (events.js:92:17)
    at emitReadable_ (_stream_readable.js:427:10)
    at emitReadable (_stream_readable.js:423:5)

这里是查询函数:

var pg = require('pg');
var connectionString = 'postgres://vagrant:vagrant@localhost:5432/catalog';

module.exports = {
    query: function(text, values, callback) {
        pg.connect(connectionString, function(err, client, done) {
            client.query(text, values, function(err, result) {
                if (err) throw err;
                done();
                callback(err, result);
            });
        });
    }
};

由以下端点调用:

app.delete(api + '/games/:id', function(req, res) {
    query('DELETE FROM videogame WHERE id=$1', [req.params.id], function(err, result) {
        if (err) throw err;
        return res.json({'success': true});
    });
});

知道是什么原因造成的吗?

【问题讨论】:

  • 您使用的是哪个版本的pg
  • 连接时没有检查err,可能出现错误时没有提供done
  • @KirillSlatin 是的,这也是我的第一个猜测,但在那种情况下,client.query() 也不起作用(我检查过)。
  • @robertklep pg@0.7.2
  • @mysterpaul 最新的是4.4.1

标签: node.js postgresql node-postgres


【解决方案1】:

解决方案正在升级到 4.4.1。

【讨论】:

  • 这是问题的原因吗? - >我只是没有处理 select 语句中缺少的中间参数。如果是这样,那么这不是您发布的问题的解决方案。
  • 该参数修复是升级后我的 SELECT 出现问题的原因;升级是原始问题的解决方案,我在使用旧版本时只有 DELETE 和 INSERT。
  • 如果您想要一个更简单的 API,而不必使用 done(),请查看 pg-promise ;)
猜你喜欢
  • 1970-01-01
  • 2017-03-26
  • 2022-10-20
  • 2021-06-11
  • 2020-10-12
  • 2015-10-29
  • 2015-06-05
  • 2019-10-16
  • 2015-06-12
相关资源
最近更新 更多