使用Promises - Node执行多个Sequelize JS模型查询方法

Dan*_*ial 5 node.js express sequelize.js mean-stack sequelize-cli

我在使用sequelize js从数据库中检索数据时遇到问题.我是NODEJS的新手.我不知道Promise和Promise.all是否内置函数所以我安装并在我的代码中也需要npm promise.以下是我的代码.

var Promise = require('promise');

var user_profile = new Promise(function(resolve, reject) {
    db.user_profile.findOne({
        where: {
            profile_id: new_profile_id
        }
    }).then(user => {
        console.log('Summary Result User found.');
        resolve(user);
    });
});

var all_reports = new Promise(function(resolve, reject) {
    db.report.all().then(reports => {
        console.log('Summary Result Reports found.');
        resolve(reports);
    });
});

var report_details = new Promise(function(resolve, reject) {
    db.report_detail.findAll({
        where: {
            profile_id: new_profile_id
        }
    }).then(report_details => {
        console.log('Summary Result Report Details found');
        resolve(report_details);
    });
});

var all_promises = Promise.all([user_profile, all_reports, report_details]).then(function(data) {
    console.log('**********COMPLETE RESULT****************');
    console.log(data);
}).catch(err => {
    console.log('**********ERROR RESULT****************');
    console.log(err);
});
Run Code Online (Sandbox Code Playgroud)

我想获取所有三个查询的数据.当我单独运行它时,我得到了数据,但是当我在Promise.all中运行它时,我只得到user_profile数据而其他两个仍未定义 我也尝试用.then嵌套这些查询但结果仍然相同我只得到一个查询数据其他两个仍未定义

然后chainging

var results = [];
var new_profile_id = req.params.profile_id;
console.log(new_profile_id);
db.user_profile.findOne({
    where: {
        profile_id: new_profile_id
    }
}).then(user => {
    console.log('Summary Result User found.');
    results.push(user.dataValues);
    return user;
}).then(user => {
    db.report.all().then(reports => {
        console.log('Summary Result Reports found.');
        results.push(reports.dataValues);
        return reports
    });
}).then(reports => {
    db.report_detail.findAll({
        where: {
            profile_id: new_profile_id
        }
    }).then(report_details => {
        console.log('Summary Result Report Details found');
        results.push(report_details.dataValues);
        console.log('**********COMPLETE RESULT****************');
        console.log(results);
        console.log('**********COMPLETE RESULT****************');
        return report_details;
    });
});
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我这个概念我做错了什么.谢谢

Eye*_*Eye 10

最新版本的Node.js已经支持Promise本机,也是如此Sequelize.这意味着无需promise单独要求.

以下代码基于您的.

const user_profile = db.user_profile.findOne({
    where: {
        profile_id: new_profile_id
    }
});

const all_reports = db.report.all();

const report_details = db.report_detail.findAll({
    where: {
        profile_id: new_profile_id
    }
});

Promise
    .all([user_profile, all_reports, report_details])
    .then(responses => {
        console.log('**********COMPLETE RESULTS****************');
        console.log(responses[0]); // user profile
        console.log(responses[1]); // all reports
        console.log(responses[2]); // report details
    })
    .catch(err => {
        console.log('**********ERROR RESULT****************');
        console.log(err);
    });
Run Code Online (Sandbox Code Playgroud)

请注意,没有必要将Sequelize调用包裹起来,Promise因为Sequelize已经返回了promises.这样,你只需要拥有一个catch在过去Promise.all(),你失踪的所有呼叫.这意味着如果任何调用失败,resolve将永远不会调用相应的调用.反过来,这意味着Promise.all()也永远不会调用最后一个.这就是为什么最好一次处理所有错误,除非有一些自定义错误处理要求.