2

I want to map all the issue titles for a repo and render them to li in a react component. I've been able to extract only the issue title using octokat library but unfortunately that library does not play nice with some other stuff i've got going on. Also, I'm sure i can do this with es6 and the fetch api.

Original Post:

Been having a hellava time finding info on using es6 fetch / promises with the github api. I'm trying to fetch issues for a repo but i'm obviously missing something... I've used a few different libraries to try and accomplish this but I'd like to just use fetch.

Update: Now my new goal is merely to console.log the titles... baby steps.

import React from "react";

export default class extends React.Component {
  constructor() {
    super()
    this.state = {
      issues: []
    }
  }
  componentWillMount() {
    // let issueArry = []
    const url = "https://api.github.com/repos/fightplights/chode-stream/issues"
    fetch(url)
      .then(response => response) // i've tried with and without .json()
      .then(body => console.log(body))
      // this logs the same response

  }
  render() {
    return (
      <div>
        <ul>
          {here i will map this.state.issues (once i can get them =))}
        </ul>
      </div>
    )
  }
}

I know it's a simple error on my part. I'm still a fledgling. =)

...and obviously i need to render to the dom (not shown here)

@Andy_D is right that I wasn't parsing the body of the response, only the response itself (doh!) however now that i'm trying to data.map(issue => ...) I still get the error that data.map() is not a function. This leads me to believe that the data object is not the object the droids are looking for... hand waves in front of screen

Here's the response object in the first promise:

Response { 
  type: "cors", 
  url: "https://api.github.com/repos/fightp…", 
  redirected: false, 
  status: 200, 
  ok: true, 
  statusText: "OK", 
  headers: Headers, 
  bodyUsed: false 
}

But when I take that response in the next .then(data => console.log(data)) It is undefined... I have tried returning the first response => response.json() (as andy suggested) but it's already json... am i wrong?

2
  • What error message does your browser devtools console show? Or what is happening or not happening that's different from what you expect? Please use stackoverflow.com/posts/43174868/edit to edit/update your question to include that info Commented Apr 3, 2017 at 0:25
  • Maybe not set response to json? can i just pass the response along : .then(response).then()? Commented Apr 3, 2017 at 1:21

1 Answer 1

2

You need to read the JSON from the body of the response, what you are attempting to render is the full response body.

So:

fetch(url)
  .then(response => response.json())
  .then(data => data.map(...)

response.json() returns a promise, so you can .then it, the return value (data) will be the JS object/array you're looking for.
https://developer.mozilla.org/en-US/docs/Web/API/Body/json

Sign up to request clarification or add additional context in comments.

3 Comments

I'm still getting an error with your method Andy. It's telling me that data.map() is not a function. I updated my example using your method.
Inspect the network request in dev tools and look at the response. Maybe it's a 200 with no content. The fact that bodyUsed is false is suspicious.
Yeah. I concur! I may have accidentally pushed my API token to gh and had it revoked. I need to do some digging...

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.