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. But when i performing it in the function success callback it it giving me error that Property 'subscribe' does not exist on type 'void'.

How can I return the value of api from service to another .ts file

Shehram Tahir
  • 915
  • 2
  • 9
  • 19
  • As @Maximus said you have to return the data at parent(outer) level inside a nested function `return this.storage.get("USER_INFO")` – nivas Jul 18 '17 at 08:09
  • hey, consider accepting [my answer](https://stackoverflow.com/a/45160956/2545680) if it helped – Max Koretskyi Jul 18 '17 at 12:23

2 Answers2

4

You're missing return statement here:

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

However, that would still return a promise with no subscribe method. To wrap a promise result into an observable, you can use from method:

getNews(newsType : any){
    return Observable.from(this.storage.get('USER_INFO').then(right => {
Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
0

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

link: How to create an observable in Angular 2

Shehram Tahir
  • 915
  • 2
  • 9
  • 19