I try to use the native reduce()
function of JavaScript with promises in order to get items in my database with children items (associated with themselves).
My datas of the documentation table :
Table : documentation
id documentationId
01 null
02 01
03 01
04 03
05 03
06 null
07 06
08 07
My code :
async function doUpWithAssociationItSelf(id) {
var arrayOfIds = [],
arrayOfOtherIds = [],
findedDocumentation = await DocumentationModel.findOne({
where: {
id
}
}),
array = [];
arrayOfIds.push(findedDocumentation.id);
arrayOfOtherIds = await getChildOfItem(array, findedDocumentation.id);
console.log(arrayOfOtherIds);
}
async function getChildOfItem(array, id) {
var childDocumentations = await DocumentationModel.findAll({
where: {
documentationId: id
},
include: [{
model: DocumentationModel
}],
order: [['position', 'ASC']]
});
return childDocumentations.reduce(async pushInArray, array);
}
async function pushInArray(array, childDocumentation) {
console.log(array);
var childArray = await getChildOfItem(array, childDocumentation.id);
array.push(childDocumentation.id);
array.push(childArray);
return array;
}
My main goal is to get items 06,07,08
when I pass the id "06" in the function doUpWithAssociationItSelf
.
Issue :
[]
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }
Promise { <pending> }