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 trialmouhamadoudiouf
14,915 PointsNeed help on my Node.JS file
Hello, I am issuing some errors with my app.js file as it doesn't output any result when I launch it with the node command. I verified my code to make sure it matched with the teacher's code. Probably an error I am not noticing. Can anyone help me?
Here's my code:
// Problem: We need a simple way to look at a user's badge count and JavaScript points
// Solution: Use Node.js to connect to Treehouse's API to get profile information to print out
//function to print message to console
//require the module
const https = require('https');
function printMessage(username, badgeCount, point){
const message = `${username} has ${badgeCount} total badge(s) and ${point} points in JavaScript`;
console.log(message);
}
function getProfile(username){
const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {
let body = "";
response.on("data", data => {
body += data.toString();
});
response.on('end', () => {
//parse the data
const profile = JSON.parse(body);
//print the data
printMessage(username, profile.badges.length, profile.points.JavaScript);
});
});
}
const users = process.argv.slice(1);
users.forEach(getProfile);
4 Answers
KRIS NIKOLAISEN
54,972 PointsI'm assuming you had back ticks where the font has changed in your post. Other than that I compared your code with the video workspace and it looks like this line is off:
const users = process.argv.slice(1);
should be
const users = process.argv.slice(2);
Full code is below:
const https = require('https');
function printMessage(username, badgeCount, point){
const message = `${username} has ${badgeCount} total badge(s) and ${point}
points in JavaScript`;
console.log(message);
}
function getProfile(username){
const request = https.get(`https://teamtreehouse.com/${username}.json`,
response => {
let body = "";
response.on("data", data => {
body += data.toString();
});
response.on('end', () => { //parse the data
const profile = JSON.parse(body);
//print the data
printMessage(username, profile.badges.length,
profile.points.JavaScript);
});
});
}
const users = process.argv.slice(2);
users.forEach(getProfile);
Antony .
2,824 PointsHi, Mouhamadou Diouf
KRIS NIKOLAISEN is right. But you also misunderstood a part of the video wrong. It wants you to put your profile username in the node command line.
For e.g. If you right click on my name and click "Open link in new tab", it will direct you to my profile and it will show you my username on the url. It should look like this
https://teamtreehouse.com/antony
mouhamadoudiouf
14,915 PointsThanks for your answer. I understand now!
mouhamadoudiouf
14,915 PointsThanks for you prompt answer! In fact, I used the back ticks in my code. I've tried with you code in my workspace with node.js and I am getting the same outpout: an error.
Here's what's thrown out:
treehouse:~/workspace$ node newapp.js Mouhamamadou Alena Dave
undefined:1
Not found
^
SyntaxError: Unexpected token N in JSON at position 0
at JSON.parse (<anonymous>)
at IncomingMessage.response.on (/home/treehouse/workspace/newapp.js:18:52)
at emitNone (events.js:111:20)
at IncomingMessage.emit (events.js:208:7)
at endReadableNT (_stream_readable.js:1064:12)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickCallback (internal/process/next_tick.js:181:9)
treehouse:~/workspace$
KRIS NIKOLAISEN
54,972 PointsTry:
node newapp.js mouhamadoudiouf
I tested it and it worked for me:
mouhamadoudiouf has 127 total badge(s) and 3756 points in JavaScript
mouhamadoudiouf
14,915 PointsYes right! The name I outputed doesn't exist in Treehouse, my bad. Everything is working now. Thanks!