hyunwooks
hyunwooks

Reputation: 141

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:[email protected]/icm";
var client = new pg.Client(pgConString);
// PG update
        pg.connect(pgConString, (err, client, done) => {
            // Handle connection errors
            if(err) {
                  done();
                  console.log(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() {
              done();
              //return res.json(results);
            });
        });

why occur this error?

pg install error?

thanks.

Upvotes: 3

Views: 6424

Answers (1)

vitaly-t
vitaly-t

Reputation: 25840

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.

Upvotes: 6

Related Questions