1

When i try to get result from node-postgres from my express app, pool.query is returning undefined result when i log it in console. Not sure if its about database connected properly or I am not returning the result properly? I am using heroku to deploy the app and using connection string given by heroku. Cant figure it out, anyone there to help please?.

db.js:

const { Pool } = require('pg');
const conString = process.env.DATABASE_URL;
const pool = new Pool({
    connectionString: conString,
        ssl: {rejectUnauthorized: false}
});
module.exports ={
    getResult: (sql, params, callback)=>{
        pool.query(sql, [params], (error, results)=>{
            console.log(results);
            if(!error){
                callback(results);
            }else{
                callback(null);
            }
            pool.end();
        });
    }
}

user-model.js

var db = require('./db');

module.exports ={
    findUserById: (userId)=>{
        return new Promise((resolve, reject)=>{
            var sql = "select id from users where id=?";
            db.getResult(sql, [userId], (result)=>{
                if(result.length>0){
                    resolve(true);
                }else{
                    resolve(false);
                }
            });
        });
    }
}
0

3 Answers 3

1

seems the sent query parameter is in mysql format, use node-postgres format which is var sql = "select id from users where id = $1"; which should return valid result

Sign up to request clarification or add additional context in comments.

Comments

1

It seems that your use of params is not correct.

You're passing an array to db.getResult(), then using it as the first element of another array.

Should just be pool.query(sql, params, (error, results)=>{ on that line.

Comments

1

you need to get the pool connection

const pool = require('./pool');

module.exports = {
  // my version
  findUserById(sql, params) {
    return new Promise((resolve, reject) => {
      return pool
        .connect()
        .then(conn => {
          conn
            .query(sql, [params])
            .then(result => {
              conn.release()
              resolve(result)
            })
            .catch(error => {
              conn.release()
              reject(error)
            })
        })
    })
  },
  
  // your version
  findUserByIds: (userId) => {
    return new Promise((resolve, reject) => {
      var sql = "select id from users where id=?";
      db.getResult(sql, [userId], (result) => {
        if (result.length > 0) {
          resolve(true);
        } else {
          resolve(false);
        }
      });
    });
  }
}

//// in you main or you controller file
   // use the function
const { findUserById } = require('./model')

app.get('/user/:id', (req, res) => {
  let sql = 'select * from "users" where "userId"= $1';
  findUserById(sql, 1)
  .then(result => {
    res.status(200).send({
      data: result
    })
  })
  .catch(error => {
    res.status(400).send(error)
  })
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.