Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialLuke Dawes
9,739 PointsNode.js: I keep getting the "Unexpected token N in JSON at positon 0" message, even after updating my code.
Hi all,
I understand that http.STATUS_CODES[response.statusCode]
is no longer the way to access the message that explains a status code error. Even after updating my code to reflect what I believe is the up-to-date method, my code still returns "Unexpected token N in JSON at position 0" when I alter my username to a non-existent one.
Solutions to this in the existing posts feel ambiguous -- I'm unsure if I should be using response.statusCode' or
response.statusMessage'. I've used the latter, but I've tried both and I still don't get my custom error message.
// Problem: We need a simple way to look at a user's Badge Count and JavaScript points on Treehouse
// Solution: Use Node.js to connect to Treehouse's API to get profile information to print out.
var https = require("https");
var username = "lucasdawes123";
// Print out message
function printMessage(username, badgeCount, points) {
var message = username + " has " + badgeCount + " total badge(s) and " + points + " points in JavaScript.";
console.log(message);
}
// Print out error messages
function printError(error) {
console.error(error.message);
}
// Connect to the API URL (https://teamtreehouse.com/username.json)
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response) {
var body = "";
// Read the data
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
if (response.statusCode = 200) {
try {
var profile = JSON.parse(body);
printMessage(username, profile.badges.length, profile.points.JavaScript);
} catch(error) {
// This handles parsing errors (e.g. if the JSON object wasn't found)
printError(error);
}
} else {
// Status code error
printError({message: "There was an error getting information for " + username + ": " + response.statusMessage + "."});
}
});
// Parse the data
// Print the data out.
});
request.on("error", printError);
I'm not sure what I'm missing. Help is welcome!
2 Answers
Thomas Nilsen
14,957 PointsJesus Mendoza is right.
Also - you have to fix your if-statement:
if (response.statusCode = 200) //here you setting statusCode to 200, not checking for equality
Do this instead:
if (response.statusCode === 200)
After you change that, you will receive this error-message:
There was an error getting information for lucasdawes123: Not Found.
Jesus Mendoza
23,289 PointsHey Luke,
That's happening because you are trying to find data for an unexisting user!
Luke Dawes
9,739 PointsHey Jesus!
Yes, that's right, which was exactly the error my code tries to handle! I realise I didn't make it clear that the point of my code was to catch and then address JSON-parse or server connection errors in plainer language using the console, but you're right. I thought I'd posted this somewhere closely tied to the video where I started experiencing the issue, but I appreciate your comment!
Jesus Mendoza
23,289 PointsNo problem man we are here to help! Thomas was right though
Luke Dawes
9,739 PointsLuke Dawes
9,739 PointsHey Thomas,
Thank you so much for your comment! I can't believe I missed the correct
if
statement syntax. Jesus was right, I was looking for a non-existent user, but that's exactly the error case I'm trying to address using theprintError
function. Thank you again! It works perfectly.