Search code examples
javascriptrestpromisegithub-api

How do I access to the value enclosed inside [[]] (double large bracket) from my promise result?


I am working on the Github OAuth Authentication button. The button when clicked will get access token from my Github Account to get me verified for my application.

To get my "access token" I have to send a request using a custom middleware and a custom "code", the GitHub will exchange this code and provide me the "access token"(according to the Github API).

What is working??
I am able to get the code and put this code in my "fetch", and able to get the access token in the object as well.

My code for fetching the token :

fetch(
    `https://gitfund-oauth.herokuapp.com/authenticate/${CODE}`
  ).then((response) => console.log(response.json()))

What is the issue?
I am getting a response, where the access token is enclosed inside the double large bracket, so how do I get that? I am getting a response as:

enter image description here

How do I get the token from the object? I tried to do the response.json().PromiseValue, but it did not work. If anybody can explain what is the double large bracket and how can I access the value, and what notation like this means in the promise chain.

Any help would mean a lot. Thank You


Solution

  • response.json() returns a promise that resolves with the result of parsing the body text as JSON. You will need to chain another .then() on it to get back the resolved promise value like:

    fetch(`https://gitfund-oauth.herokuapp.com/authenticate/${CODE}`)
      .then((response) => response.json())
      .then((data) => console.log(data))
    

    Demo:

    fetch('https://jsonplaceholder.typicode.com/todos/1')
      .then(response => response.json())
      .then(data => console.log(data))

    For more info: Using Fetch