3

I want to use the fetch() API to load data from a URL in a React Native Application. Specifically, I want to load all items from the Hackernews Jobs page.

As stated in their API doc, the corresponding call has to go out to URL: https://hacker-news.firebaseio.com/v0/jobstories.json

When navigating to this URL in the browser, I receive a simple javascript array of IDs as expected:

[11379939,11379294,11378554,11377293,11375582,11373919,11372195,11371135,11369985,11369460,11361426,11357881,11356669,11354578,11351786,11350273,11349328,11347911,11346192,11343590,11342818,11342137,11341262,11340129]

However, when I want to load the data in my React Native app using the following method, I don't receive the same javascript array I get when sending the request through the browser.

export const fetchJobs = () => {
  return (dispatch) => {
    return fetch('https://hacker-news.firebaseio.com/v0/jobstories.json', {
      method: 'GET',
      headers: {
        'Accept': 'application/json'
      }
    })
    .then( (response) => {
      console.log('Actions - fetchJobs - received response: ', response)
      const json = JSON.parse(response)
    })
    .then( (jobs) => {
      console.log('Actions - fetchJobs - received jobs: ', jobs)
      dispatch(receiveJobs(jobs))
    })
    .catch( (error) => {
      console.warn('Actions - fetchJobs - recreived error: ', error)
    })
  }
}

I am calling fetchJobs() using dispatch from Redux in my initial React component like so:

componentDidMount() {
   var fetchJobsAction = fetchJobs()
   console.log('JobsRootComponent - fetch jobs: ' + fetchJobsAction)
   const { dispatch } = this.props
   dispatch( fetchJobs() )
}

However, when inspecting the output in the Chrome debugging console, the output looks like this:

enter image description here

Can someone tell me why the content of the response differs between the requests I am sending from the browser and the one from my React Native app?

Update: As requested in the comments, I now printed response.json() as well, giving me the following result: enter image description here

So indeed, the array data now seems to be there. But I still don't understand how I can access it from where I am at the moment...

3
  • 1
    can you try to print response.json()? Commented Mar 29, 2016 at 8:18
  • sure, I just did! will update my post with the additional info! Commented Mar 29, 2016 at 8:20
  • try to add .then((response) => response.json()) to your chain, this should convert your response to json Commented Mar 29, 2016 at 8:30

2 Answers 2

5

The response of a fetch has it's own JSON parse function, just call response.json() which will return a new Promise.

fetch('https://hacker-news.firebaseio.com/v0/jobstories.json')
  .then(reponse => response.json())
  .then(json => console.log(json))
Sign up to request clarification or add additional context in comments.

Comments

4

Use return inside then

You have to return the result of response.json(), if you are chaining then calls.

export const fetchJobs = () => {
  return (dispatch) => {
    return fetch('https://hacker-news.firebaseio.com/v0/jobstories.json', {
      method: 'GET',
      headers: {
        'Accept': 'application/json'
      }
    })
    .then( (response) => {
      console.log('Actions - fetchJobs - received response: ', response)
      return response.json();
    })
    .then( (jobs) => {
      console.log('Actions - fetchJobs - received jobs: ', jobs)
      dispatch(receiveJobs(jobs))
    })
    .catch( (error) => {
      console.warn('Actions - fetchJobs - recreived error: ', error)
    })
  }
}

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.