Node.js – Route.get() requires callback functions but got a [object Undefined] after using two get request

expressnode.js

i am learning node and i have two get request in my index.js

var express = require('express');
var router = express.Router();


router.get('/', function(req, res, next) {
    res.status(200)
      .json({
        status: 'success',
        message: 'Welcome to EverestApi!'
      });
});



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

router.get('/api/users', db.getAllUsers);
router.get('/api/pref', db.getAllPref);


module.exports = router;

and my query.js

var promise = require('bluebird');

var options ={
    promiseLib : promise
};

var pgp = require('pg-promise')(options);
var db = pgp({
    host: 'localhost',
    port: 5432,
    database: 'nari',
    user: 'postgres',
    password: 'pes'
});


function getAllUsers(req, res, next) {
  db.any('SELECT * FROM office.users')
    .then(function (data) {
      res.status(200)
        .json({
          status: 'success',
          data: data,
          message: 'Retrieved all users'
        });
    })
    .catch(function (err) {
      return next(err);
    });
}


function getAllPref(req, res, next){
  db.any('SELECT * FROM core.preferences')
    .then(function(data){
      res.status(200)
        .json({
          status: 'success',
          data : data,
          message : 'Retrieved all preferences'
        });
    })
        .catch(function(err){
          return next(err);
        });
    
}


module.exports ={getAllUsers: getAllUsers};
module.exports={getAllPref : getAllPref};

I have two GET Action in here.One getAllUsers() and another getAllPref().Starting the server gives error .

D:\node-postgress-promises\node_modules\express\lib\router\route.js:202
throw new Error(msg);
^

Error: Route.get() requires callback functions but got a [object Undefined]
at Route.(anonymous function) [as get] (D:\node-postgress-promises\node_modules\express\lib\router\route.js:202:15)
at Function.proto.(anonymous function) [as get] (D:\node-postgress-promises\node_modules\express\lib\router\index.js:510:19)
at Object. (D:\node-postgress-promises\api\index.js:17:8)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object. (D:\node-postgress-promises\server.js:6:11)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)

If i comment one of the above routes for eg . router.get('/api/users', db.getAllUsers);
I get the response..Any Help please..I know this is naive but i am still learning and very new to this.Pardon if too naive.Thank you

Best Solution

in your query.js

export the functions in a object

module.exports = {
  getAllUsers: getAllUsers,
  getAllPref: getAllPref,
}
Related Question