Javascript TypeError:无法读取属性';长度';未定义的angular js客户端的
我有一个Web Api,它返回以下jsonJavascript TypeError:无法读取属性';长度';未定义的angular js客户端的,javascript,angular,rest,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": 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客户端,在收到来自的请求后,我得到了“无法读取未定义”错误的属性“长度”(让tweet of resp.json().data)属于以下方法
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>;
});
}
getCurrentFeed():可观察{
返回此.http.get('/GetTweets').map((resp:Response)=>{
log(resp.json());
var fetchedTweets=[];
for(让我们推特resp.json().data)
{
fetchedTweets.push(this.getTweetFromJson(tweet));
}
以数组的形式返回fetchedTweets;
});
}
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`;
}
}
导出类Tweet{
公共化身;
构造函数(public id:number,public body:string,public author:string,public date:date,public retweets:Array,public favorites:Array)
{
this.avatar=`${author}.jpg`;
}
}
从中删除数据
for(let tweet of resp.json().data)
{
fetchedTweets.push(this.getTweetFromJson(tweet));
}
修复了我的问题
resp.json()
是数组,为什么需要一个数据
属性?删除数据修复了我的错误,感谢您的建议resp.json()
返回一个承诺,即您必须然后退出。