I´m trying to use fetch to load some server data. Here is the code:
fetch('/user', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-type': 'application/json'
},
body: JSON.stringify({
data: 'test'
})
})
.then(response => {
if (response.status !== 200) {
console.log('Fetch status not OK');
}
else {
console.log('Fetch ok');
console.log(response); // Undefined here
}
})
.catch(error => {
console.log('network error');
});
At the browser (network) I can see the response payload being returned from server, but my response contains undefined. I can imagine this shall be simple, but I can´t find out what is happening here.
/api? What is your endpoint? Where in theapifolder are you trying to access?elsestatement// Undefined here- no it isn't - but you haven't accessed the actual response body yet ... i.e.response.json()would seem to be appropriate for your code - be aware that your.thenresults in undefined as you aren't returning anything from it - so, later in the promise chain is where you'll be getting undefined (this isn't ALL your code, right)