-1

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?

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • 2
    Often this error occurs when the server responses with an error page, which is HTML, and `<` is the first character in `` tag. Open the Network tab in the DevTools, and check what really was responsed. – Teemu Nov 26 '21 at 06:42
  • 3
    First off, it's not safe to assume the entire response arrived in one `data` event. This could be a partial response where you need to accumulate all the data events before parsing. Second off, a simple `console.log(data)` before you parse it will show you what you are getting. This is a basic debugging step that should be done BEFORE you post here. – jfriend00 Nov 26 '21 at 07:07
  • Read [ask] - you’re hoping for free help by asking here, it’s only polite for you to do due diligence by checking everything you can before asking. Part of your due diligence should be searching to find out if anyone else has seen similar problems which if you searched here for _json.parse syntax error_ that would have given you 396 results to check for possible solutions. There’s no evidence in your question that you did _any_ research; readers of your question aren’t telepathic so can only assume if you didn’t mention research that you hadn’t done any. Go figure. – DisappointedByUnaccountableMod Nov 26 '21 at 19:06
  • I guess the "Any ideas where to begin troubleshooting" was not clear enough. Next time I'll clear up the comment. Maybe something in the lines "I am not wanting an answer for my specific issue, but more an idea on how to troubleshoot it". Thank you for your support. –  Nov 26 '21 at 19:14
  • @Teemu Thank you for that huge hint! Because of that I was able to figure it out. –  Nov 26 '21 at 19:15
  • My experience is that the single best way for you to learn is to go searching and how to search well because there are very few really new problems. Second is to learn how to debug problems yourself. Arguably those could be reversed. Third(?) is asking a question here, and when you do ask a question make sure you do those steps in [ask] so you ask a good question. – DisappointedByUnaccountableMod Nov 26 '21 at 19:19
  • 1
    @SomeRandomDude Stack Overflow is for every programmer. However, we expect that you have some programming knowledge before posting a question here, otherwise you will definitely end up asking a topic that has already been asked before. Please note that Stack Overflow is not a help desk or a personal tutoring system. – Dharman Nov 26 '21 at 22:09
  • 2
    Please add code and data as text ([using code formatting](//stackoverflow.com/editing-help#code)), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and [many more reasons](//meta.stackoverflow.com/a/285557). Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. – Dharman Nov 26 '21 at 22:10
  • StackOverflow is open to everyone, but start by searching! I use it all the time to find answers, and rarely ask. Searching is getting value from StackOverflow, just as wider searching can give you information very easily/cheaply. There are already very many potential answers to your specific question, and probably most of these will implicitly or explicitly reveal that the questioner could have avoided asking by making even a very small debugging effort such as examining the text they’re trying to decode as JSON because it clearly _isn’t JSON_ if it can’t be decoded. Use StackOverflow! – DisappointedByUnaccountableMod Nov 26 '21 at 22:38

3 Answers3

0

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.

jaybhatt
  • 550
  • 2
  • 7
0
if(data != null){
  console.log(JSON.Parse(data);
}

Check the data.

Fatih Has
  • 1
  • 2
  • Please explain why he should test the data, just saying "check the data" does not help him to figure out what went wrong and why. – Esdras Xavier Nov 26 '21 at 08:53
0

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

Negi Rox
  • 3,828
  • 1
  • 11
  • 18