Home > Software design >  JSON.parse() Error SyntaxError: Unexpected token < in JSON at position 0
JSON.parse() Error SyntaxError: Unexpected token < in JSON at position 0

Time:11-26

Ok so here is the scenario. I'm following along this udemy course on fullstack webdev. Fun so far until I hit this snag.

This lesson is about API and JSON and we are making a "Sign Up" email service using Mailchimp API.

I have the following constant:

constant JSON

And when using nodemon this is the error I get:

enter image description here

That app.js 43:24 points to the: console.log(JSON.parse(data)); line.

I've checked my api key and that's correct, the options and url are set correctly.

I've been scrathing my head over this one.

Any ideas where to begin troubleshooting?

CodePudding user response:

Can you do console.log(data) once and validate that data is not undefined.

If we do JSON.parse(undefined), we get the same error as the one you are getting. You can put an if block to see if data is not undefined and then selectively parse.

CodePudding user response:

if(data != null){
  console.log(JSON.Parse(data);
}

Check the data.

CodePudding user response:

You don't need of JSON.parse(data) it because the data is already in JSON.

  • Related