I would like to ask if someone can give me explanation or check of javascript code for this case:
- I call my API to get access token and I have it.
- Next, I want to use this token to make another calls to the API for specific data.
- I need to get fields from API like: description, color, dimensions.
Example to verify below
function Get() {
var myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", `Bearer ${token.then((data)=>{data.access_token})}`);
var requestOptions = {
method: 'GET',
headers: myHeaders,
redirect: 'follow'
};
};
const first = fetch("https://XXXXX", requestOptions)
const second = first.then(response => response.json())
return second;
}
console.log('Show data', data)
Result is – illegal return statement. But I think I missed part of code to select fields from API.
3
Answers
maybe you put the curly braces wrong so the fetch can’t use the correct header, please try this code
also make sure you already store or get the token first so you can attach it to Bearer token
I call my API to get access token and I have it.
Next, I want to use this token to make another calls to the API for specific data.
I need to get fields from API like: description, color, dimensions.
It would be better if the "token" function is provided. I assume "token" is a function to get the token from a storage.
The lines
and
are not getting the response json object but just the promise.
It will be much easier to read if we modify the code in async/await function