question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

onResolve doesn't trigger if response is empty

See original GitHub issue

Hello,

Thanks for the library 😃

I’m looking at the useFetch hook and specifically the associated onResolve callback to make a call to my backend and redirect to another URL if the call is successful using this pattern:

let { ... } = useFetch(`${url}`, withAuth(meta, token), {
   defer: true,
   onResolve: data => {
     console.log("Just resolved", data);
     // Redirect will go here
   }
 });

I have absolutely no problem reaching my console.log if I return data in my response in my Express backend like so:

return res.json({
    status: "ok"
});

However, sending an empty 200 response like so:

return res.sendStatus(200);

never triggers the onResolve callback and the console.log is never printed.

Is this the intendend behavior ?

If it is, maybe editing the documentation and mentionning the required data in the response would be helpful.

I’m happy to have a look and PR if needed 😃

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:14 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ghengeveldcommented, Jan 2, 2020

We should change the automatic json deserialization to look at Content-Type instead of Accept. The former indicates what the server response contains, the latter indicates what kind of request body it accepts. Looking at the Accept header is a mistake.

Additionally, I would like to extend the json deserialization to error responses as well. Many servers return a json-encoded error message in the body for error responses, which currently is hard to retrieve with useFetch.

1reaction
phryneascommented, Jan 2, 2020

As your withAuth returns the "Accept": "application/json" header, useFetch assumes that the response from the server is JSON and tries to parse it as JSON. But the server returns an empty response - that cannot be parsed as JSON by JavaScript.

Try running JSON.parse(""), you’ll get SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data as error (slightly different behaviour in other browsers).

Either your server needs to return valid JSON or your headers should not indicate that the server should return JSON.

I’d say all of this is exactly expected behaviour: the server is returning invalid JSON although you specificly requested JSON from the server.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Don't show multi binding tool tip when empty or null
But when "MyClass" is null, I really want the tool tip to stop showing. I get around this with the following datatrigger: <DataTrigger...
Read more >
Promise.prototype.then() - JavaScript - MDN Web Docs
The then() method of a Promise object takes up to two arguments: callback functions for the fulfilled and rejected cases of the Promise....
Read more >
Don't send email if the flow body is empty
I have put the condition expression before sending email and try but didn't work as expected. Please let me know how to validate...
Read more >
Plugins - esbuild
If the plugin doesn't provide one, esbuild's default behavior won't resolve any imports in this module. This directory will be passed to any...
Read more >
Cannot invoke method getId() on null object
Hello, Can someone please help me in fixing this error? import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate import.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found