I want to make a list of categories in several views: visualize them as a list, load them as parameters of the product creation form ... To reuse these categories, I have to separate the DAO from the controllers. The problem is how to return returned categories in categoryDao in categoryController .
This is my competent MVC code with my problem:
// categoryDao.js var Category = mongoose.model('Category'); exports.allCategories = function() { Category.find().exec(function(err, categories) { console.log(categories); // is properly defined // I don't want to render categories like: res.render('categories', {allCategories : categories}); // insted, I want to return them from within this callback and make them available in the categoryController.js. But you know return categories; // is not quite right } // and of course console.log(categories); // is undefined } // categoryController.js var categoryDao = require ('./categoryDao.js'); exports.all = function(req, res) { // make allCategories available in a list view res.render('categories', {allCategories : categoryDao.allCategories}); } // productController.js var categoryDao = require ('./categoryDao.js'); exports.createForm = function(req, res) { // make allCategories available in a select element in the create product form res.render('create', {allCategories : categoryDao.allCategories}); } // categoryRoutes.js var categoryController = require ('./categoryController.js'); app.route('/categories').get(categoryController.all);
source share