Reputation: 867
Trying to build a restapi using nodejs I get this error
TypeError: Cannot call method 'getCategories' of undefined
My project structure is like this
models
-- dbhandler.js
node_modules
package.json
app.js
dbconnection.js
My dbhandler code is here
var db = require('../dbconnection');
var getData = {
getCategories : function() {
var sql = "SELECT * FROM categories"
db.connection.connect();
db.connection.query(sql,function(error, results, fields){
if (!error){
console.log('The solution is: ', results);
console.log('The solution fields is: ', fields);
} else{
console.log('Error while performing Query.');
}
});
}};
module.exports = getData;
And my app.js code is here :
var dbhandler = require('./models/dbhandler')
var router = express.Router();
router.get('/', function(req, res) {
dbhandler.getData.getCategories();
res.json({ message: 'hooray! welcome to our api!' });
});
Upvotes: 0
Views: 1597
Reputation: 365
It should be dbhandler.getCategories() as you are importing dbhandler as an object through this var dbhandler = require('./models/dbhandler')
Upvotes: 1