Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pendingItem.callback is not a function?

I am trying to view the first row of a table but I get the error

TypeError: pendingItem.callback is not a function at client.connect

var express = require("express");
const {Pool} = require("pg");
var app = express();
var conStr = "postgres://postgres:password@localhost:5432/postgres";
const pool = new Pool();

app.get("/", function(req, res, next) {
  pool.connect(conStr, function(err, client, done) {
    if (err) {
        console.log("not able to get connection " + err);
        res.status(400).send(err);
    }
    client.query("SELECT * FROM Users where id= $1", [1], function(err, result) {
      done();
      if (err) {
          console.log(err);
          res.status(400).send(err);
      }
      res.status(200).send(result.rows);
    });
  });
});

1 Answers

On Postgres connection Pool using connectionString

First if you are choosing to connect to Postgres using connectionURI, you need to initialize Pool with connectionString param:

const pool = new Pool({
  connectionString: connectionString,
})

Then when calling pool.connect you only need to pass callback function:

pool.connect((err, client, release) => {...});

Check documentation on Pooling and different ways of making a connection to Postgres using node-postgres package: here and here


In your case this, it should look like this:

var express = require("express");
const pg = require("pg");
const {Pool} = require("pg");
var app = express();
var conStr = "postgres://postgres:password@localhost:5432/postgres";
const pool = new Pool({
    connectionString: conStr
});

app.get("/", function(req, res, next) {
    pool.connect(function(err, client, done) {
        if (err) {
            console.log("not able to get connection " + err);
            res.status(400).send(err);
        }
        client.query("SELECT * FROM Users where id= $1", [1], function(err, result) {
            done();
            if (err) {
                console.log(err);
                res.status(400).send(err);
            }
            res.status(200).send(result.rows);
        });
    });
});
like image 132
Milan Avatar answered Sep 03 '26 07:09

Milan