gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 22:38:05 24 4
gpt4 key购买 nike

我在使用 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);
});

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

使用 then 链接

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

有人可以在这个概念上帮助我吗我做错了什么。谢谢

最佳答案

最新版本的 Node.js 已经原生支持 PromiseSequelize 也是如此。这意味着不需要单独要求 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);
});

请注意,不需要使用 Promise 包装 Sequelize 调用,因为 Sequelize 已经返回了 promises。这样,您只需要在最后一个 Promise.all() 中有一个 catch,您在所有调用中都缺少它。这意味着如果任何调用失败,相应的 resolve 将永远不会被调用。反过来,这意味着最后一个 Promise.all() 也永远不会被调用。这就是为什么最好在最后一次处理所有错误,除非有一些自定义错误处理要求。

关于node.js - 使用 Promises 执行多个 Sequelize JS 模型查询方法 - Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48376479/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com