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 trialmarco nacianceno
1,625 Pointsprocess.argv.slice don't display nothing
Im in the course of NodeJs in th video of Capturing Command Line Arguments when we use de process of argv at the end of the code
var https = require('https');
function printMessage(username, badgeCount,points){
var message = `${username} has ${badgeCount} total badge(s) and ${points} points in JavaScripts`;
console.log(message);
}
function getProfile(username){
//Conection to the treehouse API
var request = https.get(`https://teamtreehouse.com/${username}.json`, function (response){
let body = "";
//Read Data
response.on('data',data =>{
body += data.toString();
});
response.on('end', ()=>{
//Parse Data
const profile = JSON.parse(body);
//Print Data
printMessage(username,profile.badges.length,profile.points.JavaScript);
});
});
}
//IN THIS PART//
var users= process.argv.slice(2);
users.forEach(getProfile);
and then we execute de app with the node app.js instruction, the console don't display nothing. is supposed to display the total of badges per user
2 Answers
simhub
26,544 PointsHi Marco, did you pass your arguments via the command line?
$ node app.js simhub marconacianceno2
Because The process.argv property returns an array containing the command line arguments passed when the Node.js process was launched. (https://nodejs.org/docs/latest/api/process.html#process_process_argv)
Hope this helps :)
simhub
26,544 Pointshi marco, maybe i am wrong, but when Andrew Chalkley execute with
$ node app.js
he actually hardcoded the username's into the script
var users =['marconacianceno2','chalkers','alenaholligan'];
users.forEach(getProfile);
After he changed the code to execute it via the command line
var users= process.argv.slice(2);
users.forEach(getProfile);
$ node app.js marconacianceno2 chalkers alenaholligan
you can check the process.argv object any time
console.log(process.argv)
or check the video again (maybe i'm wrong) - https://teamtreehouse.com/library/capturing-command-line-arguments
marco nacianceno
1,625 Pointsmarco nacianceno
1,625 PointsYes I pass the argumnts
$ node app.js marconacianceno2 chalkers alenaholligan
but after that in the video erase dat arguments and execute only
$node app.js