1

Dears,

I'm struggling with the following issue. I try to initialise an array (userByWorkspaces[] declared at global scope) inside a method. I'm using fat arrow operator inside the method. As soon as I leave the scope of the fat arrow, I loose the content of my array.

I know it's related to the propagation of the context "somewhere in between" but I don't find the solution.

Please find the code snipped below.

Many thanks in advance for your help.

refreshItems() {

    console.log('home.component::refreshItems()--> Refreshing Todo list items');
    // iterate through all the user workspaces
    this.items = [];
    JSON.parse(localStorage.getItem('didowi-user')).workspace.forEach((workspace) => {

      console.log('home.componenet::refreshItems()--> ... from workspace: ' + workspace);
      this.databaseService.fetchItems(workspace).then (result => {
        for (let i = 0; i < result.rows.length; i++) {

          // take documents whose type === 'item' @see workspace database structure
          // if replaced by a mango query in database.service, filtering could be avoided here
          if (result.rows[i].doc.type === 'item') {


            this.items.push(result.rows[i].doc);
            console.log('-------+++++--------- items: ' + this.items);
            // this.urls[i] = URL.createObjectURL(result.rows[i].doc._attachments.file.data);
          } else if (result.rows[i].doc.type === 'user') {

            // take the opportunity to catch users registered in the workspaces (since it comes from the same database
            this.usersByWorkspaces.push({'workspace': workspace, 'list': result.rows[i].doc.list});
            console.log('-------+++++--------- userbyWorkspaces: ' + JSON.stringify(this.usersByWorkspaces));
          }
        }
        // this.itemCounter = this.items.length;
      }, error => {

        console.log('home.component::refreshItems()--> Error while fetching data at startup ' + error);
      });
      console.log('-------OUTSIDE INNER FAT ARROW --------- userbyWorkspaces: ' + JSON.stringify(this.usersByWorkspaces));
    });
    this.itemCounter = this.items.length;
    console.log('-------OUTSIDE--------- userbyWorkspaces: ' + JSON.stringify(this.usersByWorkspaces));
    console.log('-------OUTSIDE--------- items: ' + this.items);
}
UtkarshPramodGupta
  • 7,486
  • 7
  • 30
  • 54
Nicolas
  • 11
  • 1

0 Answers0