【发布时间】: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