0

I am sending a request to spotify to get an id value from them to use later in a post request.

fetch('https://api.spotify.com/v1/me', {
    headers: {
        'Authorization': `Bearer ${token}`
    }
}).then(response => {
    let jsonResponse = response.json();
});

this code returns an object that looks like this when converted to jason using response.json()

Promise {<pending>}__proto__: Promise[[PromiseStatus]]: "resolved"[[PromiseValue]]: Objectdisplay_name: "Jesse James"external_urls: {spotify: "https://open.spotify.com/user/1257754444"}followers: {href: null, total: 3}href: "https://api.spotify.com/v1/users/1257754444"id: "1257754444"images: [{…}]type: "user"uri: "spotify:user:1257754444"__proto__: Object

Now I want to extract the id value out. but when I use console.log( jsonResonse.id ) i get an undefined. How to I get that value?

Jaromanda X
  • 53,868
  • 5
  • 73
  • 87
Jesse James
  • 75
  • 1
  • 9

1 Answers1

2

response.json() returns a Promise

So, simply continue the promise chain as follows

fetch('https://api.spotify.com/v1/me', {
    headers: {
        'Authorization': `Bearer ${token}`
    }
})
.then(response => response.json()) // response.json() returns a Promise
.then(jsonResponse => {
    // here jsonResponse is the json you were looking for
});
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87