In my Group model, I'm exporting one function that pass the result of a query using a callback.
In my router file, I'm calling this function, after properly requiring the other file.
That's my ./models/groups.js:
var groupSchema = new mongoose.Schema({
[...]
module.exports.getAll = function(cb) {
groupSchema.find({}, function(err, groups) {
if (err) return cb(err)
cb(null, groups)
})
}
[...]
module.exports = mongoose.model('Group', groupSchema);
and that's my ./routes/groups.js file.
var Group = require('../models/group')
router.route('/groups')
.get(function(req, res, next) {
Group.getAll( function(err, group){
if (err) res.send(err)
res.send(group)
})
})
[...]
This is not working, because every time I make a get request, I'm getting a TypeError: undefined is not a function error. I know I can make the query right on my router file, but I think separating things between routes and methods would be a better practice.