0
データベースに階層をロードしようとしています。私はすべての行が親を持つことができるように、私のテーブルにparentIdを持つ列を持っています。しかし、私は再帰と約束を使って問題を抱えています。だから、sequelize/nodejsを使用するherarchyクエリ
function read (options) {
return serviceItemAttributeModel.findOne({
id: options.id,
id_organization: options.idOrganization
})
.then((attribute) => {
if (attribute) {
return loadChildren(attribute, attribute);
} else {
return attribute;
}
});
}
function loadChildren (root, attribute) {
return serviceItemAttributeModel.findAll({
where: {
id_parent: attribute.id
}
})
.then((attributes) => {
if (!attributes) {
return root;
} else {
attribute.serviceItemAttributes = [];
attributes.forEach(function (each) {
attribute.serviceItemAttributes.push(each);
return loadChildren(root, each);
});
}
});
}
、私はを呼び出す再帰(エンティティの子供たちを見ることによって)すべてのエンティティをロードしようと、私は未定義の値を取得するためにloadChildrenを呼び出すことをお読みください。何か案は?
私もコンソールでエラーが表示されます:約束はハンドラで作成されましたが、返されませんでした。
EDIT:Nosyaraヘルプ後に、このソリューション場合
を思い付きました。ありがとう!:
function read (options) {
return serviceItemAttributeModel.findOne({
where: {
id: options.attributeId,
id_organization: options.idOrganization
}
})
.then((attribute) => {
if (!attribute) {
return new Promise(function (resolve, reject) {
resolve(attribute);
});
} else {
return new Promise(function (resolve, reject) {
attribute.queryCount = 1;
resolve(attribute);
})
.then((attribute) => loadChildren(attribute, attribute));
}
});
}
function loadChildren (root, attribute) {
return new Promise(function (resolve, reject) {
return serviceItemAttributeModel.findAll({
where: {
id_parent: attribute.id
}
})
.then((attributes) => {
attributes.length = attributes.length || 0;
root.queryCount = root.queryCount - 1 + attributes.length;
if (root.queryCount === 0) {
resolve(root);
} else if (root.queryCount > 10) {
let error = new Error('Service attribute hierarchy cant have more then 10 levels');
error.statusCode = 500;
reject(error);
} else {
attribute.serviceItemAttributes = [];
attributes.forEach(function (each) {
attribute.serviceItemAttributes.push(each);
return loadChildren(root, each).then(() => {
resolve(root);
});
});
}
});
});
}
私はあなたのように見えるようにコードを変更し....しかし、それはまだdidntの仕事 – fredcrs
は、いくつかの進歩.....まだカントロード第三レベル – fredcrs
コードこれまでに得た:http://pastebin.com/Na5NHkMm – fredcrs