-1

I have a fetch request and it works fine. My question is when I try to log a specific response I get undefined but when I log the whole response I get the string printed correctly. How can I call and log a specific var from the back end. I tried console.log(respone.line) but as I said it logs undefined.

//Backend


function getInfo() {
  const line = "this is test"
  return line
}

async function callGetInfo() {
  const data1 = await getInfo()
  return data1
}

module.exports = {
  callGetInfo
};
//Frontend

function fetcher() {

  fetch(`http://localhost:PORT/api/routePath`)
    .then((res) => {
      return new Promise((resolve, reject) => {
        resolve(res.json())
      })

    }).then((response) => {
        //displays the response
        console.log(response)
        //displays undefined
        console.log(response.line)
      }

    }

  fetcher()
hamtonko
  • 49
  • 6
  • The response is a string. It doesn't have a property `line`. –  May 13 '21 at 21:44
  • For this to work, your server would need to return an Object (as JSON) with a property `line`. Your example is returning a simple String (which does not have a property `line`) – blex May 13 '21 at 21:44
  • Why are you returning a new Promise? Just `return res.json()`, which is itself a promise. – Heretic Monkey May 13 '21 at 21:44
  • That "backend" code does nothing; it doesn't listen to any ports; it doesn't return JSON to any request. It just defines a couple of functions, one of which asynchronously calls a synchronous function for some reason. – Heretic Monkey May 13 '21 at 21:48
  • Is this an assignment or a coding challange for a job interview? I'm pretty sure I've already seen a very similar code with the same problems, e.g. unnecessary `await` in `await getInfo()`, a string as repsonse instead an object, the same variable and function names. I'm very sure there is an answered duplicate with the same problem and a very similar code (if it's not deleted). –  May 13 '21 at 21:49
  • Found it: https://stackoverflow.com/questions/67299119/display-string-on-client-side-by-fetching-data-from-server-side Is this somehow related? It's a duplicate I think. –  May 13 '21 at 21:57
  • Yes, it was related. Can You please review my edited question. @jabaa – hamtonko May 13 '21 at 22:22
  • Please don't change a question in a way it invalidates valid answers. You asked a specific question and the answer answers your question. –  May 13 '21 at 22:25
  • Oh, I edited it because my first question was not clear enough. @jabaa – hamtonko May 13 '21 at 22:27
  • Your first question was very clear. The server response was a string and you expected/wanted an object with the property `line`. –  May 13 '21 at 22:28
  • then why did a mod close the question with ```This question needs details or clarity```. That why I edit it. I will delete this question and re-ask it. @jabaa – hamtonko May 13 '21 at 22:31
  • @hamtonko That's not how Stack Overflow works. On Stack Overflow users can vote to close. 3 users didn't understand the question and voted to close. I immediately understood the question because it's a duplicate of a question I've already answered (it took me 15 minutes to remember that fact). –  May 13 '21 at 22:32
  • Oh got it, Didn't know it was automated. @jabaa – hamtonko May 13 '21 at 22:37

1 Answers1

0

The response is a string. It doesn't have a property line.

console.log(response) // prints "this is test"
console.log(response.line) // property line doesn't exist.

You could return an object

function getInfo() {
  const line = "this is test";
  return { line };
}