query.on is not a function

前端 未结 3 1619
梦谈多话
梦谈多话 2021-02-13 11:48

I am trying to learn how to use javascript to connect to a postgresql database but when I try to log a query to the console using query.on(...), I get a type error that says \"q

3条回答
  •  粉色の甜心
    2021-02-13 12:15

    this is how it works for me :

    var pg = require("pg");
    
    var connectionString = {
      user: 'user',
      host: 'host',
      database: 'db',
      password: 'pass',
      port: 5432,
    };
    
    var pool = new pg.Pool(connectionString);
    
    pool.connect(function(err, client, done) {
    
        const query = client.query(new pg.Query("SELECT * from products"))
        query.on('row', (row) => {
            console.log(row);
        })
        query.on('end', (res) => {
            // pool shutdown
            console.log("ending");
            pool.end()
        })
        query.on('error', (res) => {
            console.log(res);
        })
    
        done()
    })
    

    source : https://node-postgres.com/features/connecting

提交回复
热议问题