5
我想学习如何使用JavaScript连接到postgresql数据库,但当我尝试使用query.on(...)将日志查询记录到控制台时,我得到一个类型错误,说“query.on不是一个函数”。我已经广泛搜索了如何解决这个问题,但似乎无法找到.on函数的任何文档。我知道连接是成功的,因为当我从终端查询数据库时,已经添加了两个新行。query.on不是函数
jsontest.js
var pg = require('pg');
var conString = "postgres://[username]:[password]@localhost:5432/VONKTA1";
//username and password masked
var client = new pg.Client(conString);
client.connect();
client.query("INSERT INTO json_test (name, attributes) VALUES ('Ted', $1)", [{"age": 2, "gender": "M"}]);
client.query("INSERT INTO json_test (name, attributes) VALUES ('Sarah', $1)", [{"age": 8, "gender": "F"}]);
console.log("about to query");
var query = client.query("SELECT * FROM json_test");
query.on('row', function(row) {
console.log(row);
});
query.on('end', function() {
client.end();
});
的package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"pg": "^7.0.2",
}
}
而没有任何知识什么'pg.Client'是,你能不能只是简单地控制台登录查询' '?你可以看到有什么方法可用,或许在创建新的'pg.Client'对象时有错误? – jdmdevdotnet
'query.on'已从node-pg 7中删除。有关如何正确处理行,请参阅https://node-postgres.com/guides/upgrading。 –
从来没有使用过这个库,但是从一瞥来看,我没有在文档中看到关于'.on'的任何内容。他们正在使用回调,承诺或“等待/异步”而不是事件 – baao