Home > database >  response is working but result is undefined
response is working but result is undefined

Time:02-28

I have a simple POST request using fetch()

fetch('/login', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ email: '[email protected]', password: 'test' }),
    })
      .then((response) => {
        response.json();
      })
      .then((result) => {
        console.log(result);
      });

the response is valid and gives the JSON that I am expecting { 'id':1 } but the result just returns undefined every time. Have I written something incorrectly or am I missing something really simple?

CodePudding user response:

try return response on the first then:

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