我是node.js的新手。我正在尝试将csv文件值加载到数据库中。它显示错误如下:
错误:
在
var stream = client.query(copyFrom('COPY menu_list FROM STDIN'));
TypeError: Cannot call method 'query' of null at /home/sathish/backup2/backup/sappsApli/routes/index.js:487:23 at /home/sathish/backup2/backup/sappsApli/node_modules/pg/lib/pool.js:75:25 at /home/sathish/backup2/backup/sappsApli/node_modules/pg/node_modules/generic-pool/lib/generic-pool.js:274:11 at /home/sathish/backup2/backup/sappsApli/node_modules/pg/lib/pool.js:27:26 at null.<anonymous> (/home/sathish/backup2/backup/sappsApli/node_modules/pg/lib/client.js:172:5) at EventEmitter.emit (events.js:95:17) at Socket.<anonymous> (/home/sathish/backup2/backup/sappsApli/node_modules/pg/lib/connection.js:109:12) at Socket.EventEmitter.emit (events.js:95:17) at Socket.<anonymous> (_stream_readable.js:710:14) at Socket.EventEmitter.emit (events.js:92:17)
我的代码:
var fs = require('fs');
var pg = require('pg');
var copyFrom = require('pg-copy-streams').from;
pg.connect(function(err, client, done) {
console.log("welcome");
var stream = client.query(copyFrom('COPY menu_list FROM STDIN'));
var fileStream = fs.createReadStream('list.csv')
fileStream.on('error', done);
fileStream.pipe(stream).on('finish', done).on('error', done);
});
任何人都可以帮助破解这个问题吗?
答案 0 :(得分:-1)
如果您没有使用构造函数初始化客户端,则需要将连接字符串传递给pg.connect()
。
var conString = "postgres://username:password@localhost/database";
pg.connect(conString, function(err, client, done) {
{...}
});
或者:
var client = new pg.Client(conString);
client.connect(function(error) {
{...}
});
如果您检查了错误回调,它会告诉您问题是什么。