4

I have this error :

TypeError: db.any is not a function

I'm using node JS, with pg-promise and express. Here is the queries.js file (db) :

const promise = require('bluebird');

var options = {
    promiseLib: promise
};

var pgp = require('pg-promise')(options);
var connectionString = 'postgres://localhost:5432/spendy';
var db = pgp(connectionString);

console.log('connexion ok');

The controller :

const db = require('../queries');

module.exports = {
    getAllUsers: function(req, res, next) {
        db.any('select * from users')
            .then((data) => {
                res.status(200)
                    .json({
                        status: 'success',
                        data: data,
                        message: 'Retrieve all users'
                    });
            })
            .catch((err) => {
                console.log(err);
                return next(err);
            });
    }
}

And the routes file :

var express = require('express');
var router = express.Router();
var db = require('../queries');
const UserController = require('../controller/UserController');

router.get('/users', UserController.getAllUsers);

module.exports = router;

I use pg-promise with version 6.7.1 and bluebird 3.5.0, I don't understand where is the error, if someone could help me,

thank you !

Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33
Antonin Mrchd
  • 656
  • 3
  • 9
  • 31

2 Answers2

6

You dont appear to be exporting your db in the queries file. Try inluding this in queries

module.exports = db;
Lucas Hendren
  • 2,786
  • 2
  • 18
  • 33
2

you don't export your module queries

http://openmymind.net/2012/2/3/Node-Require-and-Exports/

sheplu
  • 2,937
  • 3
  • 24
  • 21