Error cb.apply is not a function from PostgresSQL Database Action Script Template

I am trying to connect to my PostgresSQL database hosted on Heroku through Auth0’s Database Connections. I am following the postgresSQL template as closely as possible; however, I am getting an Error:

cb.apply is not a function

for my Create script.

Here is the script:

function create(user, callback) {
  //this example uses the "pg" library
  //more info here: https://github.com/brianc/node-postgres

  const bcrypt = require('bcrypt');
  const postgres = require('pg');

  const conString = configuration.DATABASE_URL;
  postgres.connect(conString, {ssl: { sslmode: 'require', rejectUnauthorized: false}}, function (err, client, done) {
    if (err) return callback(err);

    bcrypt.hash(user.password, 10, function (err, hashedPassword) {
      if (err) return callback(err);

      const query = 'INSERT INTO users(email, password) VALUES ($1, $2)';
      client.query(query, [user.email, hashedPassword], function (err, result) {
        // NOTE: always call `done()` here to close
        // the connection to the database
        done();

        return callback(err);
      });
    });
  });
}

I had to declare the sslmode in the postgres.connect because of this problem I was having with ssl.

It would be great if I can get help on where I can go from here.

I was able to get help by opening a ticket and what resolved the issue was declaring the version number of pg as well as updating the template. Here is the code that worked for me (Note that my table is named organizations)

function create(user, callback) {
  //this example uses the "pg" library
  //more info here: https://github.com/brianc/node-postgres

  const bcrypt = require('bcrypt');
  const postgres = require('pg@8.7.1');

  const conString = configuration.DATABASE_URL;
  const client = new postgres.Client( {
    connectionString: conString,
    ssl: {sslmode: 'require', rejectUnauthorized: false}
  });
  client.connect();
  
  bcrypt.hash(user.password, 10, function (err, hashedPassword) {
    if (err) return callback(err);

    const query = 'INSERT INTO organizations(email, password) VALUES ($1, $2)';
    client.query(query, [user.email, hashedPassword], function (err, result) {
      // NOTE: always call `done()` here to close
      // the connection to the database
      client.end();

      return callback(err);
    });
  });
}
1 Like

Thanks for sharing that with the rest of community!