-1

i am wondering why the code below logs undefined to the console? i have already eliminated a backend issue because when i use the broswers developer tools to inspect this request everything seems fine and the response shown in the dev tools is the json data i was expecting. any help appreciated.

const url = window.location.href.split('/')
const url1 = url[6].substring(2)

fetch(`http://localhost:8080/apiget/userinfo/${url1}`)
.then(response=>{
   response.json()
}) 
.then(json=>console.log(json))
.catch(error=>console.log('something went wrong'))


yusufa22
  • 21
  • 1
  • 4

1 Answers1

2

The code below returns undefined because you are not returning anything

fetch(`http://localhost:8080/apiget/userinfo/${url1}`)
  .then(response=>{
    response.json();
  });

so JavaScript will return undefined to next then chain. You can change this code block like this:

.then(response => response.json());

or

.then(reponse => {
  return response.json();
});