最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TS2334 Property 'length' does not exist on type 'Object'. on app start only (ng-ser

programmeradmin1浏览0评论

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 ponent 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'. on a for loop I have on my response array.

I've tried iterating through the response, only if it's not null, but that didn't work.

function:

dailyForecast() {
  var token = {token : "0a1b2c3d"};
  return this._http.post("", token)
  .map(result => result);
}

I get the error inside this for loop, but it works fine after I re-pile.

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

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 ponent 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'. on a for loop I have on my response array.

I've tried iterating through the response, only if it's not null, but that didn't work.

function:

dailyForecast() {
  var token = {token : "0a1b2c3d"};
  return this._http.post("https://www.improving..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-pile.

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]);
        }
      }
    });
}
Share Improve this question edited Oct 16, 2018 at 14:08 user5734311 asked Oct 16, 2018 at 14:01 queroga_vqzqueroga_vqz 1,0493 gold badges12 silver badges29 bronze badges 6
  • 1 Absolutely nothing here? google.nl/… – mplungjan Commented Oct 16, 2018 at 14:03
  • Have not found a thread where this problem only happens on initial build. – queroga_vqz Commented Oct 16, 2018 at 14:05
  • What does the data look like when it errors out? Is it changing? Check your network tab in dev tools. – Frank Modica Commented Oct 16, 2018 at 14:08
  • what does console.log(res); return as the error occur – Hana Wujira Commented Oct 16, 2018 at 14:10
  • @FrankModica dev network tab shows a generic t=1539699003903 net::ERR_CONNECTION_REFUSED – queroga_vqz Commented Oct 16, 2018 at 14:13
 |  Show 1 more ment

1 Answer 1

Reset to default 8

If you want to avoid the pile error you can set the response type to any[] or a custom interface.

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

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论