【问题标题】:orm sequelize query from two different tables in a single page loadorm sequelize 在单个页面加载中来自两个不同表的查询
【发布时间】:2017-07-10 23:04:55
【问题描述】:

我有这个问题:需要在一页(page.hbs)不同的表上显示数据。我正在使用 nodejs orm sequelize、mysql 数据库、Handlebars。当get方法到page.hbs页面需要从两个不同的表中获取数据。这是我的代码:

型号:

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);
});
}

路线:

module.exports = function (app, passport) {

app.get('/page_admin', isLoggedIn, authController.page_admin);

............

请帮忙(我不太明白如何让一个查询访问两个表,据我所知,您需要修复 authcontroller.js(merge requests) )提前感谢您的帮助。

【问题讨论】:

    标签: javascript mysql node.js sequelize.js


    【解决方案1】:

    使用Promises

    Promise.all([db.findAll(tableA), db.findAll(tableB)])
    .then((data) => {
       //data[0] is response from tableA find
       // data[1] is from tableB
    })
    

    【讨论】:

      猜你喜欢
      • 2017-11-30
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多