Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
Need to extract name value from json array of the output

I am unable to extract the name variable from the graph output of the following react code. trying to store the name value from the json output received from the api in my state variable in React. How do i do it?

state = {
        auth: false,
        username: '',
        access_token: '',
        app_name: [],
    };

    responseFacebook = response => {
        {/*console.log(response);*/}
        if(response.status !== 'unknown')
        this.setState({
            auth: true,
            username: response.name,
            access_token: response.accessToken
        });
        graph.setAccessToken(this.state.access_token);
        graph.get("/me/accounts", function(err, res) {
            let response = res;
            console.log(response.data[0]);
          });
        console.log(this.state);
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Maybe because your checking the second console.log outside the callback. In javascript the callbacks( the function inside the get call ) are trigerred later, when the API completes, hence you will not get anything in the console.log outside the callback, if you rewrite your example it might work.

state = {
    auth: false,
    username: '',
    access_token: '',
    app_name: [],
};

responseFacebook = response => {
    if(response.status !== 'unknown') {
        this.setState({
            auth: true,
            username: response.name,
            access_token: response.accessToken
        });
        graph.setAccessToken(this.state.access_token);
        graph.get("/me/accounts", (err, res) => {
            let response = res;
            this.setState({username:response.data[0]});
            console.log(this.state);
        });
    }
}

Event loop reference

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!