角度2:订阅可观察到的收益率“未定义”?

时间:2016-08-31 19:49:51

标签: angular observable

我有一个简单的服务,它获取github用户的回购并正确返回数组对象

githubservice

 getRepos(){
      return  this._http.get(this._url + this.username +"/repos")
      .map(users =>{         
             this.u = users.json();
             console.log("getRepos", this.u); //<== correctly returns array object
          });
    }

当我在组件中订阅它时,我得到了未定义。

githubcomponent

 getReposFromGithub(){
       return  this._githubService.getRepos().subscribe(repos => {
         this.repos = repos; 
         console.log("repos", repos);},  //<== logs 'undefined' 
         (error)=> console.log("error message",error), //<== no error message
         ()=>{console.log("completed");} //<== gets logged 
         );
     }


 ngOnInit() {
         console.log("on changes");
         this.getReposFromGithub();  
     }

fyi,这是返回的数组对象

enter image description here

我在这里缺少什么?可能会使用一些rxjs运算符? 提前谢谢!!

2 个答案:

答案 0 :(得分:3)

您未在.map的{​​{1}}来电中退回任何内容。您可能意味着返回结果数组。添加getRepos(),如下所示:

return this.u;

答案 1 :(得分:2)

您在map中使用getRepos时使用的lambda函数不会返回任何内容。

相关问题