Return result from http get

Hi, how can i return the result of this http get?


getChatId(emailTo) {

    var email = emailTo
    this.storage.get('token').then((val) => {
      const httpOptions = {
        headers: new HttpHeaders({
          'Accept': 'application/json',
          'Content-Type': 'application/json',
          'Token': val
        })
      };

      this.httpClient.get("https://sxxxxxx=" + email, httpOptions)
        .subscribe(data => {
          console.log(data['_body']);
        }, error => {
          console.log(error);
        });
    });

  }

Your question looks confusing.
If possible please format it correctly and explain what you want to achieve and what are you getting?
Thanks

1 Like

i have this function that make an http get request that return me an JsonObject.
i call this function inside the constructor.
how can i return this JsonObject inside my constructor? so after that i will able to call another function.

I think it should like this

token:string=’’;
result=any;

getChatId(emailTo) {
this.storage.get('token’).pipe(
switchMap(output=> {
 this.token = output.property;
 const httpOptions = {
        headers: new HttpHeaders({
          'Accept': 'application/json',
          'Content-Type': 'application/json',
          'Token': this.token
        })
      };
  this.httpClient.get("https://sxxxxxx=" + email, httpOptions)
        .subscribe(data => {
          console.log(data['_body']);
         this.result=JSON.stringify(data)

        //use this result 
        }, error => {
          console.log(error);
        });
    });
   });
    });
  }

Just check braces properly closing and opening.
hope it helps

1 Like

You may get error while getting token from nativestorage.
It returns Promise.

create one func which return observable from promise

getToken (): Observable<any> {
    return from(this.storage.get('token'));
}

Then use this getToken() in above code .

getToken().pipe(

rest of the code )

thank you so much i will try it