0

I'm trying to filter out data from fetch of an external API (CoinMarketCap) in React.

I only want the data for a specific set of tickers, but after numerous attempts I can't seem to get it to work correctly. I feel like this should be an easy fix, but can't seem to figure it out.

getData = () => {
      const tickers = ["btc", "xrp", "eth", "ltc", "bch", "etc", "str", "xmr", "nxt", "zec", "rep", "dash"];
      fetch(`https://api.coinmarketcap.com/v1/ticker`)
      .then(res => res.json())
      .then(data => data.filter(d => tickers.includes(d.symbol)))
      .then(results => console.log(results)
    ) 

But it's logging out an empty array. Any ideas?

1
  • If you are receiving data from the above API, then your array#filter callback should be filter(d => tickers.includes(d.symbol.toLowerCase())) Commented Feb 21, 2018 at 3:13

3 Answers 3

1

each then() needs a Promise or any thenable function. problem is that Array.prototype.filter() is not a promise and not resolving anything (it's not async function). you should write this:

getData = () => {
      const tickers = ["btc", "xrp", "eth", "ltc", "bch", "etc", "str", "xmr", "nxt", "zec", "rep", "dash"];
      fetch(`https://api.coinmarketcap.com/v1/ticker`)
      .then(res => res.json())
      .then(data => console.log(data.filter(d => tickers.includes(d.symbol))))
    ) 
Sign up to request clarification or add additional context in comments.

Comments

0

If you were getting the data from fetch, add toLowerCase() to the d.symbol

tickers.include(d.symbol.toLowerCase()

Comments

0

Turns out this was a CORS issue. I had to move the fetch to my backend and called from REACT component. Working fine now. Thanks.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.