我正在尝试学习如何使用javascript连接到postgresql数据库但是当我尝试使用query.on(...)将查询记录到控制台时,我得到一个类型错误,上面写着"query.on不是功能".我已经广泛搜索了如何解决这个问题,但似乎无法找到关于.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();
});
Run Code Online (Sandbox Code Playgroud)
的package.json
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "test.js",
"scripts": {
"test": "echo \"Error: no …Run Code Online (Sandbox Code Playgroud)