简体   繁体   English

角度2中的类型'void'上不存在属性'subscribe'

[英]Property 'subscribe' does not exist on type 'void' in angular 2

getNews(newsType : any){

 this.storage.get("USER_INFO").then(right=>{
 this.storage.get("sessionkey").then(temp=>{
 this.email = JSON.parse(right).email;
 this.newkey = temp;
 this.authentification =JSON.stringify("Basic " + btoa(this.email+":"+ this.newkey+":"+key));
    const body = newsType;
    let headers = new Headers({ 
      'Content-Type': 'application/json; charset=UTF-8',
      'Authorization': this.authentification
    });

    let options = new RequestOptions({headers : headers});
   return this.http.post('http://api/getNews',body,options)
    .map((data:Response) => data.json());

}, err =>{console.log("error on sessionkey",err)})
}, err =>{console.log("error on user",err)})

  }


 this.httpService.getNews(JSON.stringify(this.category)).subscribe(data => {
      this.news = data.News;
    });
    }, err => {
      console.log("Error:", err) 
    });

I want to call the Api after the success of nested functions. 嵌套函数成功后,我想调用Api。 But when i performing it in the function success callback it it giving me error that Property 'subscribe' does not exist on type 'void'. 但是,当我在函数成功回调中执行它时,它给我错误,类型“ void”上不存在属性“ subscribe”。

How can I return the value of api from service to another .ts file 如何将api的值从服务返回到另一个.ts文件

You're missing return statement here: 您在这里缺少return声明:

getNews(newsType : any){
    return this.storage.get('USER_INFO').then(right => {
    ^^^^^^

However, that would still return a promise with no subscribe method. 但是,这仍然会返回没有subscribe方法的promise。 To wrap a promise result into an observable, you can use from method: 要将promise结果包装为可观察的结果,可以使用from方法:

getNews(newsType : any){
    return Observable.from(this.storage.get('USER_INFO').then(right => {

This is the solution which worked for me. 这是为我工作的解决方案。

this.httpService.getNews(JSON.stringify(this.category)).subscribe(data => { 
      this.news = data.News;
    }}, err => {
      console.log("Error:", err) 
    });



getNews(newsType :any) : Observable<any> {
  return Observable.create(observer => { 
   this.storage.get("USER_INFO").then(right=>{
 this.storage.get("sessionkey").then(temp=>{
 this.email = JSON.parse(right).email;
  let authentification =JSON.stringify("Basic " + btoa(this.email+":"+ temp+":"+key));
      const body = newsType;
    let headers = new Headers({ 
      'Content-Type': 'application/json; charset=UTF-8',
      'Authorization': authentification
    });

    let options = new RequestOptions({headers : headers});
    this.http.post('http:api/getNews',body,options).subscribe(data =>{
      observer.next(data.json());
      observer.complete();
    });
}, err =>{console.log("error on sessionkey",err)})
}, err =>{console.log("error on user",err)})
 }) }

Thanks @Maximus and @Theophilus Omoregbee 谢谢@Maximus和@Theophilus Omoregbee

link: How to create an observable in Angular 2 链接: 如何在Angular 2中创建可观察对象

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型'void'上不存在属性'subscribe'。 Angular 2 asp.net核心 - Property 'subscribe' does not exist on type 'void'. Angular 2 asp.net core Angular。 TS2339:“无效”类型上不存在属性“订阅” - Angular. TS2339: Property 'subscribe' does not exist on type 'void' 在Angular 2 App中返回一个Observable,但仍然出现错误:“ void”类型不存在“属性“ subscribe” - Returning an Observable in Angular 2 App, but still getting error: "Property 'subscribe' does not exist on type 'void' 类型 'void | 上不存在属性 'subscribe' 可观察的<user> '。 ts(2339)</user> - Property 'subscribe' does not exist on type 'void | Observable<User>'. ts(2339) 'void'类型上不存在属性'管道' Angular 9 - Property 'pipe' does not exist on type 'void' Angular 9 类型 {} 上不存在属性“订阅” - Property 'subscribe' does not exist on type {} Angular 7中的类型&#39;OperatorFunction &lt;{},{}&gt;&#39;错误上不存在属性&#39;subscribe&#39; - Property 'subscribe' does not exist on type 'OperatorFunction<{}, {}>' error in Angular 7 Angular 2属性订阅在Promise类型上不存在<any> - Angular 2 Property subscribe does not exist on type Promise <any> 类型 void 上不存在属性 - Property does not exist on type void 在Angular2 +中,类型&#39;()=&gt; void&#39;上不存在属性&#39;nativeElement&#39; - Property 'nativeElement' does not exist on type '() => void' in Angular2+
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM