query.on is not a function

前端 未结 3 1604
梦谈多话
梦谈多话 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:32

    As Mentioned by Denys Séguret in Answer, the function query.on is deprecated. And if you are a beginner and want to get a quick connection to try out your queries without being bothered by async/await functionality. You can try the below code:-

    const { Pool, Client } = require('pg')
    const connectionString = 'postgresql://dbuser:dbpassword@database.server.com:5432/mydb'
    
    const pool = new Pool({
      connectionString: connectionString,
    })
    
    pool.query('SELECT NOW()', (err, res) => {
      console.log(err, res)
      pool.end()
    })
    

提交回复
热议问题