onResolve doesn't trigger if response is empty
See original GitHub issueHello,
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:
- Created 4 years ago
- Comments:14 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

We should change the automatic json deserialization to look at
Content-Typeinstead ofAccept. 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.As your
withAuthreturns the"Accept": "application/json"header,useFetchassumes 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 getSyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON dataas 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.