- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在使用 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 已经原生支持 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);
});
请注意,不需要使用 Promise
包装 Sequelize 调用,因为 Sequelize 已经返回了 promises。这样,您只需要在最后一个 Promise.all()
中有一个 catch
,您在所有调用中都缺少它。这意味着如果任何调用失败,相应的 resolve
将永远不会被调用。反过来,这意味着最后一个 Promise.all()
也永远不会被调用。这就是为什么最好在最后一次处理所有错误,除非有一些自定义错误处理要求。
关于node.js - 使用 Promises 执行多个 Sequelize JS 模型查询方法 - Node ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48376479/
我正在关注 Sequelize 纪录片。在 Model Definition 部分的底部,我想知道 sequelize 变量用于什么?我可以删除它吗? { sequelize, modelNa
我有这个原始 SQL 查询。 SELECT restaurants.name AS restaurant, ROUND((6371 * ACOS(COS(RADIANS(6.9271)
我有一个 postgres 数据库,我正在使用 Sequelize。从表 车辆 和 预订 我试图在给定开始日期和结束日期的情况下获得所有可用车辆。 如果我使用给定日期搜索 Bookings: Book
我正在尝试使用 HapiJS 和 Sequelize 开始一个项目,并且开始时,希望在没有 Sequelize CLI 的情况下使事情正常工作,以了解一切是如何结合在一起的。 我见过多个示例项目,例如
sequelize init:models 命令会创建一个“models”文件夹并添加一个 index.js。 有人能解释一下这个 index.js 文件到底做了什么,以及它如何适应 Sequeliz
查看此 Sequelize tutorial 中的 models/blog.js 定义. module.exports = (sequelize, type) => { return sequ
我收到一个错误 No overload matches this call 每当我尝试使用 @HasMany() 或 @BelongsTo 装饰器时。 我正在使用 nestjs/sequelize:
const gamem = await gamemodes.findAll({attributes: ['name']}); const newme = await gamemodes.fin
我们如何使用 sequelize 4.2 创建由所有模型应用/继承的自定义实例方法?在 Sequelize 3 中,我们曾经在扩展到所有其他模型的主模型的“定义”部分中有一个“instanceMeth
我正在使用 Typescript、NodeJS 和sequelize-auto-ts 进行模型生成。我想要放置包含查找查询的两个模型给了我错误的结果,这又是由于触发了错误的查询。这是有问题的两个模型;
假设我在用户表中有 5 列 name email state isOnline createdAt | N
我有两张表,其中一张表具有另一张表的 ID。 1:1 关系。 所以像 EventFeedback somePrimaryKey userEventID UserEvent us
如何使用对象创建具有现有关联的条目? 例如, User.create({ socialMedia: [{ socialId: 1, //should reference existing
如何创建在创建 Sequelize 模型的新实例时以编程方式生成的默认值?我已经在某处阅读了如何执行此操作,但在任何地方都找不到。我以为这与classMethods有关,但我不知道应该调用什么方法。
当我设置关联时,有没有办法使用 Sequelize (sequelizejs.com) 输出所有在对象上神奇创建的函数。 例如;我有一个用户模型,我设置 User.belongsToMany(User
我该如何制作 Music是`音乐? { id: 4 name: "playlist 1" created_at: "2015-04-21T21:43:07.000Z" updated_
我可以使用 sequelize 从可用模型创建数据库模式吗?我在一个缺少许多迁移且没有测试的项目上工作。要运行测试,我需要创建一个新的 db (sqlite3),但我无法使用迁移初始化其架构(因为它们
我有一个与其他模型相关联的简单 Sequelize 模型。 module.exports = function (sequelize, DataTypes) { var Votes = seque
我有一个像这样设置的协会: m.User.hasMany(m.Interests, { joinTableName: 'user_interests', foreignKey: 'user_id' }
我有以下型号: var User = Seq.define('user', { firstName: { type: Sequelize.STRING,
我是一名优秀的程序员,十分优秀!