hyunwooks hyunwooks -3 years ago 444
Javascript Question

How to fix pg.connect is not a function on node.js?

I want fix

pg.connect is not a function
on node.js.

I install postgreSQL.

my source

TypeError: pg.connect is not a function
at Socket.<anonymous> (/home/iosys/bin/cctv-sigserver-8241.js:329:20)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)
at Socket.emit (/home/iosys/bin/node_modules/socket.io/lib/socket.js:129:10)
at Socket.onclose (/home/iosys/bin/node_modules/socket.io/lib/socket.js:418:8)
at Client.onclose (/home/iosys/bin/node_modules/socket.io/lib/client.js:230:12)
at Client.onerror (/home/iosys/bin/node_modules/socket.io/lib/client.js:211:8)
at Client.ondata (/home/iosys/bin/node_modules/socket.io/lib/client.js:177:10)
at emitOne (events.js:96:13)
at Socket.emit (events.js:188:7)

var pg = require('pg');
var pgConString = "postgres://icm:test@";
var client = new pg.Client(pgConString);
// PG update
pg.connect(pgConString, (err, client, done) => {
// Handle connection errors
if(err) {
//return res.status(500).json({success: false, data: err});
// SQL Query > Update Data
var sql_update = "UPDATE becctv SET becamonoff = '1' WHERE mid = '"
+ mid + "' AND bescode = '" + scode + "' AND becamid = '"
+ camid + "'";
console.log('sql_update 000 = [ ' + sql_update + ' ]');

const query = client.query(sql_update);

// After all data is returned, close connection and return results
query.on('end', function() {
//return res.json(results);

why occur this error?

pg install error?


Answer Source

pg.connect became deprecated in v6.3.0, and removed in v7.0.0. You can still do client.connect, but the recommended approach is pool.connect - see the official documentation.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download