Hello, I have such a problem: you need to display data on the same page (page.hbs) of different tables. I use nodejs, orm sequelize, mysql db, Handlebars. With the getter method, the page.hbs page needs to get data from two different tables. Here is my code:

models:

var roof_type = sequelize.define('roof_type', { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true, allowNull: false }, description: { type: DataTypes.STRING(50), allowNull: true } }); return roof_type; var garret_type = sequelize.define('garret_type', { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true, allowNull: false }, description: { type: DataTypes.STRING(50), allowNull: true } }); return garret_type; 

authcontroller.js:

 exports.page_admin = function (req, res) { db.roof_type.findAll({ description: 'description ASC' }).then(function (data) { var hbsObject = { roof_types: data }; res.render('page_admin', hbsObject); }); } exports.page_admin = function (req, res) { db.garret_type.findAll({ description: 'description ASC' }).then(function (data) { var hbsObject = { garret_types: data }; res.render('page_admin', hbsObject); }); } 

routes:

 module.exports = function (app, passport) { app.get('/page_admin', isLoggedIn, authController.page_admin); 

.........

Please help (I do not really understand how to access two tables in one get request, as I understand it is necessary to correct authcontroller.js (merge queries)) Thanks in advance for your help.

    2 answers 2

     exports.page_admin = function (req, res) { var hbsObject = {}; var count = 0; db.roof_type.findAll({ description: 'description ASC' }).then(function (data) { hbsObject.roof_types = data; count++; if(count == 2) res.render('page_admin', hbsObject); }); db.garret_type.findAll({ description: 'description ASC' }).then(function (data) { hbsObject.garret_types = data; count++; if(count == 2) res.render('page_admin', hbsObject); }); } 

    So peysali between the "grandfather's" way and the standard 2015.

     var async = require('async'); exports.page_admin = function (req, res) { var hbsObject = {}; async.parallel([ function(callback) { db.roof_type.findAll({ description: 'description ASC' }).then(function (data) { hbsObject.roof_types = data; callback(hbsObject); }); }, function(callback) { db.garret_type.findAll({ description: 'description ASC' }).then(function (data) { hbsObject.garret_types = data; callback(hbsObject); }); }], function(err, hbsObject) { if(err) console.error(err); res.render('page_admin', hbsObject); }); }; 

    By the way, Express has a special object res.locals for collecting data for dynamic generation, and if you add to it, res.locals.roof_types = data; and res.locals.garret_types = data; - then do not need the second argument - res.render('page_admin'); - he is already there.

    • async / await is used with babel now or with the prefix --harmony (now it is not advisable to use, not a stable version). Why advise that while it is better not to use. As for me, Promises are now the best option, and yours is no good anymore. And yes, maybe they did it once, but now they don’t do it anymore and this is not considered normal code writing, so be simple. - Vladislav Siroshtan
    • But with the console, which just shows that this version of node.js is under development. Look at the LTS chart, so far it is better not to use the dev version for a prod, but soon, LTS for version 8 will be released in October, and then at least fill up with async / await - Vladislav Siroshtan

    It is better to use this:

     module.exports.page_admin = (req, res, next) => { return db.sequelize.Promise.all([ db.roof_type.findAll({ description: 'description ASC' }), db.garret_type.findAll({ description: 'description ASC' }) ]) .spread((roof_types, garret_types) => { return res.render('page_admin', { roof_types, garret_types }); }) .catch(next); }