Search code examples
sqlexpressselectresponse

i am trying to to connect my SQL table on my database with express server to send the columns in the response


so this is my code

    const url = `postgres://.......`;
const pg =require (`pg`);

 const client =new pg.Client(url);
app.get("/data", handeldata)

    function handeldata(req ,res ){
let sql = 'select * from houses ;'
client.query(sql).then((results)=>{

    
    res.send(results.rows);
}).catch((err)=>{

console.log(err);

})


}

there is no error massage only the respone on thunder is reloading or not recived and i tried to console log inside the function but it did not see it ?

check my table here "Select * from houses"


Solution

  • You forgot to create the connection:

    client.connect(); //<=====
    
    app.get("/data", handledata);
    
    function handledata(req, res) {
      let sql = "select * from houses ;";
      client
        .query(sql)
        .then((results) => {
          res.send(results.rows);
        })
        .catch((err) => {
          res.send(err);
        });
    }