我正在尝试为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);
});
});
});
}
}错误:
(节点:25079) UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝id: 3):TypeError:无法读取未定义(节点:25079)的属性“符号(Symbol.iterator)”DeprecationWarning:未处理的承诺拒绝被取消。在未来,承诺不处理的拒绝将使用非零退出代码终止Node.js进程。(节点:25079) UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝id: 4):TypeError:无法读取未定义的属性“符号(Symbol.iterator)”
发布于 2016-11-24 09:49:39
您有来自Promise.all的句柄错误,因为它还返回一个承诺,并且您需要处理它,除非您将它训练为返回的承诺。
Promise.all([...])
.then(...)
.catch(function(err) {
console.error(err);
reject(err);
});编辑:
var promiseArr = [];
Object.keys(table.associations).forEach(tbl => {
promiseArr.push(
self.includeFK(table.associations[tbl].target, depth + 1, includes)
);
});
Promise.all(promiseArr)
.then(results => {
resolve(true)
});我还认为您的this绑定不在正确的范围内。如果得到未定义函数的错误,请在调用类函数之前尝试使用变量引用this。
示例:
includeFK(table, depth, includes) {
var self = this; //ref this and use it later
...
...
self.includeFK();https://stackoverflow.com/questions/40782800
复制相似问题