简体   繁体   English

TS2334类型“对象”上不存在属性“长度”。 仅在应用启动时(ng-serve)

[英]TS2334 Property 'length' does not exist on type 'Object'. on app start only (ng-serve)

I'm having an issue when I start my angular app. 启动我的角度应用程序时遇到问题。

I have a function that does a post request, returns some data to my component and plots a chart. 我有一个函数可以执行发布请求,将一些数据返回到我的组件并绘制图表。 It works fine after I start the app do some random modification and save, but when I first start the app I get Property 'length' does not exist on type 'Object'. 启动应用程序进行一些随机修改并保存后,它可以正常工作,但是当我第一次启动应用程序时,得到的Property 'length' does not exist on type 'Object'. on a for loop I have on my response array. 在我的响应数组上的for循环上。

I've tried iterating through the response, only if it's not null, but that didn't work. 我尝试遍历整个响应,但前提是它不为null,但这是行不通的。

function: 功能:

dailyForecast() {
  var token = {token : "0a1b2c3d"};
  return this._http.post("https://www.improving.com.br/api/test/hits-by-browser", token)
  .map(result => result);
}

I get the error inside this for loop, but it works fine after I re-compile. 我在此for循环中收到错误,但在重新编译后仍然可以正常工作。

ngOnInit() {
  let chart = < any > {};
  this._weather.dailyForecast()
    .subscribe(res => {
      console.log(res);
      let browsers = [];
      let browsersAcesss = [];
      if (res !== null) {
        for (var i = 0; i < res.length; i++) {
          browsers.push(res[i][0]);
          browsersAcesss.push(res[i][1]);
        }
      }
    });
}

If you want to avoid the compile error you can set the response type to any[] or a custom interface. 如果要避免编译错误,可以将响应类型设置为any[]或自定义接口。

ngOnInit() {
  let chart = < any > {};
  this._weather.dailyForecast()
    .subscribe((res: any[]) => {
      console.log(res);
      let browsers = [];
      let browsersAcesss = [];
      if (res !== null) {
        for (var i = 0; i < res.length; i++) {
          browsers.push(res[i][0]);
          browsersAcesss.push(res[i][1]);
        }
      }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM