【发布时间】:2017-04-08 13:04:48
【问题描述】:
我正在尝试为 sequelize.js 编写一个基类。这个类将关联所有相关的表。 includeFk 函数实现了这个任务。但它有一个承诺,应该是递归的。 类:
class base {
constructor(table, depth) {
this._table = table;
this._depth = depth;
}
includeFK(table, depth, includes) {
return new Promise((resolve, reject) => {
if (depth <= this._depth) {
for (var att in table.tableAttributes) {
table.belongsTo(m, {
as: m.name,
foreignKey: att
})
includes.push({
model: m
});
}
}
Promise.all(
Object.keys(table.associations).forEach(tbl => {
this.includeFK(table.associations[tbl].target, depth + 1, includes);
}
)).then(results => {
resolve(true)
});
} else {
resolve(true);
}
});
all(query) {
return new Promise((resolve, reject) => {
var tmp = this;
var includes = [];
Promise.all([
this.includeFK(tmp._table, 1, includes),
this.includeLang()
]).then(function() {
tmp._table.findAll({
include: includes
}).then(function(dbStatus) {
resolve(dbStatus);
});
});
});
}
}
错误:
(node:25079) UnhandledPromiseRejectionWarning: 未处理的承诺 拒绝(拒绝 id:3):TypeError:无法读取属性 未定义的“符号(Symbol.iterator)”(节点:25079) DeprecationWarning:不推荐使用未处理的承诺拒绝。在 未来,未处理的承诺拒绝将终止 具有非零退出代码的 Node.js 进程。 (节点:25079) UnhandledPromiseRejectionWarning:未处理的承诺拒绝 (拒绝 id:4):TypeError:无法读取属性 'Symbol(Symbol.iterator)' of undefined
【问题讨论】:
标签: javascript node.js ecmascript-6 sequelize.js