0

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> }
tonymx227
  • 5,293
  • 16
  • 48
  • 91
  • 1
    See [this answer](https://stackoverflow.com/a/41243567/6304441). – glhrmv Mar 10 '23 at 09:58
  • 2
    The methods of [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array) do not work with asynchronous callbacks. You have to pass the array of promises to [`Promise.all()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) or to [`Promise.allSettled()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) and `await` the promise that it returns. – axiac Mar 10 '23 at 09:58
  • Ok, do you have a model with my code from above ? – tonymx227 Mar 10 '23 at 10:03
  • It'll be much easier to use a simple loop for this than `reduce` – Bergi Mar 10 '23 at 10:21
  • `Array.reduce()` does not help much here. I would try to do get the tree nodes on levels (first the root node, then all its children, next all children of its children and so on), one SQL query for each level (using `documentationId IN (...)` if the ORM provides support for it). At each step I would append the new nodes to the output list. That should complete in `N` steps (`N` queries to the database), where `N` is the maximum depth of a grand-grand-..-children in the tree. – axiac Mar 10 '23 at 10:21

1 Answers1

-1

async functions return promise you have to await them all in order to get array of results

just like @axiac said. use Promise.all(array) or Promise.allSettled(array)

  • 1
    This could be a good answer but it misses several things: sample code to suggest a solution, code formatting, links to the documentation (not necessarily in this order). What you posted here looks like a comment on the question but you cannot comment because you do not have enough reputation. In order to get reputation put some effort and provide useful answers (see some hints above). Or read [this answer](https://meta.stackoverflow.com/questions/359378/how-to-write-better-answers). Or [this article](https://codeblog.jonskeet.uk/2009/02/17/answering-technical-questions-helpfully/). – axiac Mar 10 '23 at 10:07