【发布时间】:2019-03-10 20:30:55
【问题描述】:
我正在尝试从另一个使用 youtube API 发送 HTTP 请求的组件获取信息,但我遇到了这个问题:
Cannot read property 'subscribe' of undefined
at SafeSubscriber._next (profile.page.ts:20)
这是组件代码,我从这里尝试从服务中获取信息:
constructor(private db:FirebaseService,private afauth:AuthService) {
this.db.getDataObj("/Profile/" + this.uid).subscribe(res =>{
this.profileInfo= res;
this.afauth.getYoutubeData(res.channel).subscribe(data =>{
console.log(data);
})
})}
这是发送 http 请求的服务的函数代码:
getYoutubeData(ch):any{
let m="https://www.googleapis.com/youtube/v3/channels?
part=snippet%2CcontentDetails%2Cstatistics&id=" + ch + "&key=" + api ;
this.http.get(m).subscribe(data =>
{
this.youtubeObj=data.items["0"].statistics;
return this.youtubeObj;
})
}
【问题讨论】:
-
你不能像这样从订阅中返回数据,它实际上不会返回数据。相反,您可以在 pipe() 中使用诸如 map() 之类的 RxJS 运算符将格式化的可观察流数据传递给其他方法。