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 trialBen Os
20,008 PointsNodeJS --- SyntaxError: Unexpected token N in JSON at position 0 (a bug in the console)?
As this lesson I used the following code to execute my program with node app.js chalkers alenaholligan
in the terminal.
Yet I get:
NodeJS --- SyntaxError: Unexpected token N in JSON at position 0
Here the code I used. I looked again and again and also used a debugger but I couldn't fin any syntax error:
function printMessage(username, badges, points) {
let message = `${username} has ${badges} total badge(s) and ${points} points in Javascript.`;
console.log(message);
}
// printMessage('chalkers', 100, 2000000);
let https = require('https');
function getProfile(username) {
let request = https.get(`https://teamtreehouse.com/${username}.json`, (response)=> {
let responseBody = '';
response.on("data", (dataChunk)=> {
responseBody += dataChunk.toString();
});
response.on("end", ()=> {
let profile = JSON.parse(responseBody);
printMessage(username, profile.badges.length, profile.points.JavaScript);
});
request.on("error", (error)=> {
console.error(error.message);
});
});
}
let users = process.argv.slice(1); // cut one of the 3.
users.forEach(getProfile);
Why won't it work? Might it be a bug in the console?
2 Answers
Alexander La Bianca
15,959 PointsAll slice(2) does is get rid of the two items in argv that are not usernames. Those are the ones you saw when you logged the argv value. After you start entering values in the console, your argv will contain the values you entered. The slice method does have nothing to do with how many usernames you entered. It is very common actually in node to do the argv.slice(2) as the first two items are not used in most console node based applications.
So when you start before you pass in anything. Your argv will always be:
//argv
[ '/usr/local/nodejs-binary-6.11.1/bin/node', '/home/treehouse/workspace/app.js' ]
//argv[0] is '/usr/local/nodejs-binary-6.11.1/bin/node'
//argv[1] is '/home/treehouse/workspace/app.js'
Then you start entering usernames so after you enter the first username the argv value is
//argv
[ '/usr/local/nodejs-binary-6.11.1/bin/node', '/home/treehouse/workspace/app.js', 'chalkers' ]
Now when you do your slicing with 1 instead of 2 you are assigning a non username into your users array
let users = process.argv.slice(1)
//now users is: ['/home/treehouse/workspace/app.js', 'chalkers']
Now you are doing your forEach loop with that users array. Well the first item in the users array is clearly not a username that is on treehouse. So treehouse will give you back 'Not Found' which is not valid JSON. That's why you get the error
But if you do slicing with 2 you will only grab anything in the argv array that is AFTER the first two items
let users = process.argv.slice(2)
//now users is: ['chalkers']
Now you only have valid usernames.
Check our the Array.slice() method on MDN for clarity. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/slice
Alexander La Bianca
15,959 PointsThe issue is your line:
let users = process.argv.slice(1);
//Needs to change to
let users = process.argv.slice(2);
You got the error because you also searched for a username of the value at argv[1] which is the path of the file that is running the node.js program. Which is why the teamtreehouse api returned NOT FOUND which is invalid JSON. I see you had the exact same error because it states:
NodeJS --- SyntaxError: Unexpected token N in JSON at position 0 N is the N of the NOT FOUND
console log the process.argv value and you see what I mean
Ben Os
20,008 PointsSadly your answer isn't clear to me. When I print process.argv with console.log I get the following stdout in execution:
[ '/usr/local/nodejs-binary-6.11.1/bin/node', '/home/treehouse/workspace/app.js' ]
In slice I wrote 1 instead of 2 because I gave only two usernames instead of three (as in Chalk's original example). I did so by means of minimalism --- To bring one of the two with slice, but not the two, as in:
node app.js chalkers alenaholligan
And yet id I change the slice() integer to 2 (even if I pass 2 names or three names), it works.
So, I still miss why the integer passed to slice() make any difference. I mean, I still miss why 1 won't work but 2 will.
Ben Os
20,008 PointsBen Os
20,008 PointsThanks, I now understood that.