Could not use Subscribe method when data is getting returned from Storage.get

I’m new to Angular 4 and I’m doing my best to learn all this new stuff and for the last 2 days I’m stuck with this.

After Logging In I want to pass the API Token as a query string to my backend server with every requests.I don’t know how to achieve it. I tried using some snippets that I found on stackoverflow but those are throwing new errors. Hope someone could help me with this. Here’s my HTTP provider’s code.

import { Injectable } from '@angular/core';
import * as AppConfig from '../../app/app.config';
import {Storage} from '@ionic/storage';
import { Http, RequestOptions, URLSearchParams } from '@angular/http';
/*
  Generated class for the HttpProvider provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular DI.
*/
@Injectable()

export class HttpProvider {
....

  get(url:string){
    let params: URLSearchParams = new URLSearchParams();
    console.log(this.token);
    let requestOptions = new RequestOptions();
    requestOptions.search = params;
    this.storage.ready().then(() => {
      this.storage.get('token').then(token => {
      params.set('api_token', this.token);
        return this.http.get(url, requestOptions);
      })
    });
  }

}

Can someone help me with this?

Read the official Angular docs on Http. There are several code examples there. People post here all the time after following out-of date (or just bad) tutorials they found online. I don’t personally try to clean up that code, though there are some hardier souls here who sometimes do. But I don’t recommend relying on random blogs or Stack Overflow answers. Even if they were right at the time they were written, they might be outdated by now. Read official docs, and source code of official Github projects.

The most important thing you need to change stylistically is to write in TypeScript, not Javascript. The Angular Http docs talk about the importance of type checking the result of the get operation. If you don’t do that sort of thing, you risk bad runtime errors.

Declare all return value types and you will discover what is wrong.

thanks for the reply Aaron. I’ve been trying to solve this for past 2 days. I did read those Angular Docs but still don’t know what is wrong with my code.