Javascript /在粗箭头功能内丢失上下文

时间:2018-07-22 14:31:29

标签: javascript

亲爱的

我正在努力解决以下问题。 我尝试在方法内部初始化数组(在全局范围内声明的userByWorkspaces [])。我在方法内部使用了胖箭头运算符。离开粗箭头的范围后,我立即松开数组的内容。

我知道这与上下文“在两者之间的某个地方”的传播有关,但是我找不到解决方法。

请在下面找到代码片段。

在此先感谢您的帮助。

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);
}

0 个答案:

没有答案