I'm happy to join the StackOverFlow community.
I am new to web development and have billions of questions.
My question will concern a fetch request in javascript.
I'm trying to extract the data (userId) from the response in order to export it but I can't.
I tried to set the userId variable to global but it doesn't work.
Is there anyone who could help him on this issue.
Thank you in advance for your answers.
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();
Use the fetch() method to return a promise that resolves into a Response object. To get the actual data, you call one of the methods of the Response object e.g., text() or json() . These methods resolve into the actual data.
Approach: First make the necessary JavaScript file, HTML file and CSS file. Then store the API URL in a variable (here api_url). Define a async function (here getapi()) and pass api_url in that function. Define a constant response and store the fetched data by await fetch() method.
To get JSON from the server using the Fetch API, you can use the response. json() method. The response. json() method reads the data returned by the server and returns a promise that resolves with a JSON object.
You can get more data from the request, refer to the documentation. POST request using fetch API: The post request is widely used to submit forms to the server. Fetch also supports the POST method call.
You need to return response.json() in your first then. Then you chain another then to that one where you receive your data (if any was sent to you). Be sure that the data you receive is json.
fetch(url, parametresDeRequete)
.then(function(response) {
if (response.status !== 200) {
console.log(
"Looks like there was a problem. Status Code: " + response.status
);
return;
} else {
return response.json(); // returns unresolved Promise
}
}
.then(function(data) { // data refers to the resolved promise. If the response is not json then the previous .json() method will throw an error which you will be able to catch with .catch()
userId = data.id;
console.log(
"%c ⚠️ Utilities Logged User Id ⚠️ ===>>",
"color:red ; font-size: 15px",
userId
);
});
})
.catch(function(err) {
console.log("Fetch Error :-S", err);
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With