In my Node.js / Express.js I make a call to a model (implemented in Sequelize) which is supposed to, for example, get a category or a list of all categories.
Now in Sequelize finders work like this:
Model.findAll().on('success', function(models) { /* foo */ })
Model.findAll().success(function(models) { /* foo */ })
In both cases they don't return anything directly, they only pass fetched models to .success()
function. I would like to pass them back to the router where they're initially called.
If I do in the model,
return Model.findAll().success(function(models) { /* foo */ });
router receives undefined
. But I do not want to pass router code as a callback to Sequelize because later in the router I want to do something else to the data and maybe fetch other models.
Any help regarding this would be appreciated.
UPDATE:
Example of a route, that renders this page: http://dev3.itemscope.com/c
module.exports = {
GET : {
index: function(req, res) {
GLOBAL.routes.setLastVisitedPage(req);
var render = function(models) {
if (models !== null) {
if (req.accepts('html')) {
res.render('../views/category/index', {
title: 'Categories',
heading: 'Categories',
categories: models,
breadcrumbs: [
{
href: '/',
text: 'Home'
},
{
text: 'Categories'
}
],
sidebarAdSlot: 'homepage_300x250'
});
return;
}
if (req.accepts('json') || req.accepts('javascript')) {
return res.json(models);
}
}
else {
if (req.accepts('html')) {
res.render('../views/category/index', {
title: 'Categories',
heading: 'No categories found',
sidebarAdSlot: 'homepage_300x250'
});
return;
}
if (req.accepts('json') || req.accepts('javascript')) {
return res.json(null);
}
}
}
Category.getAll(render);
return;
}
}
}