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 trial

JavaScript Node.js Basics 2017 Handling Errors in Node Organizing Your Code with require

Przemyslaw Mazur
PLUS
Przemyslaw Mazur
Courses Plus Student 9,296 Points

Don't know how to pass two different arguments to the command line.

I have modified the get function to take two arguments. One of them is topic but when I type in node app.js chalkers JavaScript the app is reading the second argument as another user.

Here is a snapshot of my workspace https://w.trhou.se/8ydc2e87oh .

Please help.

2 Answers

Farid Wilhelm Zimmermann
Farid Wilhelm Zimmermann
16,753 Points

After you slice the process.argv array, you call users.forEach(profile.get). The forEach method is iterating over each of the members of your users array, passing its callback parameter (which is each element of your users array) to the profile.get function. That way you are invoking profile.get multiple times, which leads to multiple API calls in turn. For every time the forEach iterates over your users array, it only passes down 1 parameter to the profile.get function.

To fix this, you should do something like this.

const profile = require('./profile.js');

const users = process.argv.slice(2);
profile.get(users[0], users[1])

^ if you are expecting only two command line arguments

const profile = require('./profile.js');

const users = process.argv.slice(2);
for (let i = 0; i < users.length; i+= 2) {
  profile.get(users[i], users[i + 1])
}

^if you have an unknown number of users and topics you want to retrieve. However, this will lead to an error, if you are not passing in the proper number of command line arguments. Hope it was understandable!

this is just what I was looking for. Thanks!

James Estrada
seal-mask
.a{fill-rule:evenodd;}techdegree
James Estrada
Full Stack JavaScript Techdegree Student 25,866 Points

You could also use destructuring and a for...of loop to go through each username for a particular topic, for example:

const profile = require('./profile.js');

//take the first element of the array as the topic and the rest as an array of usernames
let [topic, ...usernames] = process.argv.slice(2);
//go through each username and pass the same topic to each one of them as arguments to the get method
for (let username of usernames){
  profile.get(topic, username);
}

You can modify the above to add more topics and loop through each one of them as well.

Fabian Forsström
Fabian Forsström
7,978 Points

I like this answer! Thank you James :)