0

I have a service that I get an http from API

export class DataDisplayFromAPI {

result:any;

constructor(private _http: HttpClient) {}

getPrices() {
    return this._http.get("https://min-api.cryptocompare.com/data/pricemulti?fsyms=BTC,ETH,LTC,BCH,IOT,XRP&tsyms=USD")
    .map(result => this.result = result);
}
}

What I try to do is to replace the coins list BTC,ETH,LTC,BCH,IOT,XRP inside the string with a variable, as follow:

export class DataDisplayFromAPI {

result:any;
coinsList = 'BTC,ETH,LTC,BCH,IOT,XRP';

constructor(private _http: HttpClient) {}

getPrices() {
    return this._http.get("https://min-api.cryptocompare.com/data/pricemulti?fsyms=" + coinsList + "&tsyms=USD")
    .map(result => this.result = result);
}
}

But get error.

I tried also

return this._http.get(`https://min-api.cryptocompare.com/data/pricemulti?fsyms=${coinsList}&tsyms=USD`)

But it also doesn't work

How can I do it?

Thanks

2
  • does it returns any error? and check out you network log and see the route that you are requesting to Commented Jan 1, 2018 at 18:17
  • What error? Give a minimal reproducible example. Commented Jan 1, 2018 at 18:22

1 Answer 1

3

coinsList is not defined in the method's scope. You need to refer to the component's scope with this

getPrices() {
    return this._http.get("https://min-api.cryptocompare.com/data/pricemulti?fsyms="
    + this.coinsList +
    "&tsyms=USD")
    .map(result => this.result = result);
}

or

getPrices() {
    return this._http.get(`https://min-api.cryptocompare.com/data/pricemulti?fsyms=${this.coinsList}&tsyms=USD`)
        .map(result => this.result = result);
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.