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 !
You dont appear to be exporting your db in the queries file. Try inluding this in queries
module.exports = db;
you don't export your module queries
http://openmymind.net/2012/2/3/Node-Require-and-Exports/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With