TypeError:无法读取未定义的角度js客户端的属性“长度”

时间:2017-07-11 18:19:47

标签: javascript angular rest

我有一个Web Api,它返回一个以下的json。

[
  {
    "id": 2,
    "body": "This is my first Tweet",
    "author": "Kumar",
    "date": "2017-07-11T00:00:00+05:30",
    "retweets": [
      "Mahela",
      "Sanath"
    ],
    "favorites": [
      "Sanath"
    ]
  },
  {
    "id": 3,
    "body": "This is my first Tweet",
    "author": "Virat",
    "date": "2017-07-11T00:00:00+05:30",
    "retweets": [
      "Mahela",
      "Sanath"
    ],
    "favorites": [
      "Sanath"
    ]
  }
]

我有一个Angular.js客户端,在收到请求后,我从获取'无法读取属性'长度'未定义'错误(让resp.json()。数据的推文所属以下方法。

getCurrentFeed() :Observable<Tweet[]>{


  return this.http.get('/GetTweets').map((resp :Response)=>{


    console.log(resp.json());

    var fetchedTweets=[];

    for(let tweet of resp.json().data)
    {
     fetchedTweets.push(this.getTweetFromJson(tweet));

    }
    return fetchedTweets as Array<Tweet>;
});

}

tweet.ts位于

之下
export class Tweet {

public avatar;



constructor( public id :number ,public body :string ,public author :string,public date:Date,public retweets:Array<string>,public favorites:Array<string>)
{
 this.avatar=`${author}.jpg`;

}
 }

1 个答案:

答案 0 :(得分:0)

中删除数据
for(let tweet of resp.json().data)
{
 fetchedTweets.push(this.getTweetFromJson(tweet));

}

解决了我的问题