Greetings to the wonderful StackOverFlow community.
As a beginner in web development, I have countless questions swirling in my mind.
The focal point of my query is regarding a fetch request in JavaScript.
I am struggling to extract the data (userId) from the response for exporting purposes.
Despite attempting to set the userId variable as global, I am facing issues.
If there is anyone out there who could lend a helping hand on this matter, it would be greatly appreciated.
Many thanks in advance for your valuable insights!
let userId = "";
let loggedUserId = () => {
let storageToken = localStorage.getItem("groupomania");
let objJson = JSON.parse(storageToken);
let token = objJson.token;
let params = token;
const headers = new Headers();
headers.append("Authorization", `Bearer ${token}`);
let url = "http://localhost:3000/api/user/userId/" + params;
const parametresDeRequete = {
method: "GET",
headers: headers,
};
fetch(url, parametresDeRequete)
.then(function(response) {
if (response.status !== 200) {
console.log(
"Looks like there was a problem. Status Code: " + response.status
);
return;
}
response.json().then(function(data) {
userId = data.data;
console.log(
"%c ⚠️ Utilities Logged User Id ⚠️ ===>>",
"color:red ; font-size: 15px",
userId
);
});
})
.catch(function(err) {
console.log("Fetch Error :-S", err);
});
};
loggedUserId();