【问题标题】:can't access value from inside then to outside then Promise无法从内部访问值然后到外部然后 Promise
【发布时间】:2019-07-05 12:31:19
【问题描述】:

我试图从内到外获得价值,然后使用承诺。

在我的控制器中,我有:

async Sample(userId: number)
  {
      let data = this.userService.getAffectedExpertisesPromise(userId);
      await data.then((uri) =>
      {
          uri.forEach((exp: Expertise) =>
          {
              this.empList.push(exp.name);
          });
      })

      return this.empList;
  }

ngOnInit上,我调用这个函数:

this.Sample(25).then(item =>
                    {
                        item.forEach((expLibelle: String) =>
                        {
                            listExp.push(expLibelle);
                        });
                        console.log("------------------------ List Size Inside: " + listExp.length);
                    });

      console.log("------------------------ List Size Outside : " + listExp.length);

服务用户文件中,我有:

getAffectedExpertisesPromise(id: number): Promise<any>
    {
        return this.http.get(`${this.baseUrl}/users/expertisesObj/${id}`).toPromise();
    }

它产生:

------------ 列表大小外部:0

------------ 列表大小内部:3

如你所见:

  • then里面的大小是3 -->正确答案
  • then 里面的大小为 0 --> 错误答案

你能帮我解决这个问题吗?

非常感谢。

【问题讨论】:

标签: angular6


【解决方案1】:

为什么不尝试使用XMLHttpRequest

var request = new XMLHttpRequest();
request.open('GET', 'http://localhost:6227/api/auth/users', false);  
request.send(null);

HTH。

【讨论】:

  • 你好小姐@SariaEssid。这也有效。非常感谢。
猜你喜欢
  • 1970-01-01
  • 2017-12-15
  • 1970-01-01
  • 1970-01-01
  • 2014-01-31
  • 1970-01-01
  • 2015-09-28
  • 2019-01-21
  • 2018-01-31
相关资源
最近更新 更多