-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')) 
1
  • 2
    This is a typo. response.json() is not being returned. Commented Jan 19, 2021 at 13:14

1 Answer 1

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(); }); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.