Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node express & pg-promise - db.any is not a function

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 !

like image 422
Antonin Mrchd Avatar asked Jan 30 '23 13:01

Antonin Mrchd


2 Answers

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

module.exports = db;
like image 187
Lucas Hendren Avatar answered Feb 02 '23 03:02

Lucas Hendren


you don't export your module queries

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

like image 31
sheplu Avatar answered Feb 02 '23 05:02

sheplu