Search code examples
javascriptnode.jsjsonherokufetch

Parsing JSON on Firefox vs. Chrome (error: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data)


I've built a small application in node.js and javascript using the twitter API that gets a keyword input from the user and then displays some recent tweets with that keyword.

It works fine in localhost on all browsers. But when I deploy to Heroku, it only works in Chrome.

In Firefox, I get this error: Uncaught (in promise) SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data.

I am receiving an array of condensed tweets where each element in the array is in in a form like {"text": "this is a tweet", "user": "@username", "url": "twitter.com/this-tweet"}. It is the array itself that I have done JSON.stringify on.

So my question in particular is what I might need to do to fix this, but more generally I am curious as to why my parsing of JSON would work fine on Chrome but not on Firefox.


Solution

  • If you look at the error message, you'll see that it's getting caught on the very first character, which means the string that's being fed to JSON.parse() doesn't even start with a valid character, like "{". Try logging out what the server is giving back to you - you'll probably quickly find the issue by doing so.

    One likely scenario, if you're using fetch, is that the server is actually returning an error (something with a 4xx or 5xx status code). fetch() doesn't throw on status-code errors, only network errors, so if you're not properly checking the error code, you might be trying to interpret the error message as JSON data.