0

For some reason my Express server returns this rather than a string like it is supposed to? Sorry if it sounds weird, but i am new to this development, is my data somewhere in this CORS header? Am i missing something? Previously i was encountering some issues linked to "No 'Access-Control-Allow-Origin'..." error. Through that i was told to use CORS, but I'm ​unsure on how to unpack this response, is my data in this response?

Image of error im getting

Thank you for your kind help :

Phil
  • 157,677
  • 23
  • 242
  • 245
Yash Rraj Sood
  • 75
  • 1
  • 10
  • 1
    please post your error in the question. Also note that this isnt an error. Its an image of a response with a 200 status code. Please show the code how you made the request. Otherwise its not possible to tell how to access the data. – The Fool Feb 17 '22 at 07:36
  • @derpirscher `mode: "cors"` is [the default](https://developer.mozilla.org/en-US/docs/Web/API/Request/Request#:~:text=origin%2C%20or%20navigate.-,The%20default%20is%20cors,-.) and OP's request was successful. Their mistake was logging the [Response](https://developer.mozilla.org/en-US/docs/Web/API/Response) object – Phil Feb 17 '22 at 07:46
  • Please see https://css-tricks.com/using-fetch/. It's an excellent guide – Phil Feb 17 '22 at 07:52

1 Answers1

1

You are console logging just the Response. You need to convert your Response to Json then console log that, if your server is responding with JSON. For instance, your fetch Request would look similar to this.

.then(function (response) {
        const data = response.json();
        return data;
      })
      .then((data) => {
      console.log(data);

then in the console.log(data) object you can look for your response, however it is structured.

EX.

console.log(data.response)

or

console.log(data.msg)

You should also catch any errors with a "Catch" handler as such


.catch((err) => console.log(err));

at the end of your Request.

  • 2
    That's assuming OP's API responds with JSON. They said they're expecting _"a string"_. You're also failing to check for non-successful response statuses – Phil Feb 17 '22 at 07:44
  • 1
    I don't have enough reputation to ask the OP for those clarifying questions unfortunately., so I was just trying to help. I would also hope he knows to check for non-successful response statuses, since that wasn't the issue he was having. – DrakeColeman Feb 17 '22 at 07:48
  • 2
    OP has the literal equivalent of `fetch("http://localhost:3000/coin/BTC").then(console.log)` so I don't think they know what to do with the response at all – Phil Feb 17 '22 at 07:51
  • 1
    Right. Yeah, again, I'm trying to help, not hinder. I edited my response, what do you think? – DrakeColeman Feb 17 '22 at 07:59