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

Neil McPartlin
Neil McPartlin
14,662 Points

'Data type' problem when querying a JSON object

Towards the end of this video, Andrew suggests a challenge where in the console we can enter node app.js JavaScript chalkers. So the challenge is to modify the current code to achieve this.

  1. Recognise that the 3rd entry is now a topic and not a user.
  2. Recognise that users will be 4th entry onwards.
  3. Modify the print message so that the points read are for the subject entered in the console.

1 & 2 I have done, but with 3 I am either very close or miles away.

So at the console I have entered: node app.js JavaScript chalkers

It correctly shows: chalkers has 209 total badge(s) and 5946 points in JavaScript

but this is because I have not yet been able to change the 'printMessage' line.

Here is the code extract.

// Parse the data

const profile = JSON.parse(body);
const topic = process.argv.slice(2)[0];
const subject = `profile.points.${topic}`;

// Print the data

printMessage(username, profile.badges.length, profile.points.JavaScript);

console.log(topic); // JavaScript  
console.log(typeof topic); // string 
console.log(username); // chalkers 
console.log(typeof username); // string 
console.log(profile.badges.length);// 209  
console.log(typeof profile.badges.length);//  number
console.log(profile.points.JavaScript);// 5946 
console.log(typeof profile.points.JavaScript);// number
console.log(subject);// profile.points.JavaScript
console.log(typeof subject);// string

So in the printMessage line, I had hoped to be able to replace profile.points.JavaScript with subject but the data type of subject is wrong.

Is there a simple data conversion I need to make or is the approach wrong?

Neil McPartlin
Neil McPartlin
14,662 Points

Sorry to trouble you Steven Parker but could you give me a steer on this please?

The profile in question for reference is Andrew's [https://teamtreehouse.com/chalkers.json] and I understand how we use dot notation in the 'printMessage' line to read his JavaScript points, but I can't figure out how to incorporate a variable instead. I would happily 'google' this but I don't have the right terminology to hand.

Thanks in advance.

3 Answers

Steven Parker
Steven Parker
231,275 Points

I get it, you want to be able to specify a specific topic on the command line, and then display the points for that topic for a number of users. You can do that using the bracket notation for property access instead of dot notation.

So you could expand printMessage to take and display an additional parameter for the topic:

//Function to print message to console
function printMessage(username, badgeCount, points, topic) {
  const message = `${username} has ${badgeCount} total badge(s) and ${points} points in ${topic}.`;
  console.log(message);
}

And then you could modify the call to it on line 44:

          printMessage(username, profile.badges.length, profile.points[topic], topic);

And you can eliminate "subject" as it was not being used.

One other odd thing I noticed on line 40:

          // this seems a bit complicated:
          const topic = process.argv.slice(2)[0];
          // since it essentially does the same as this:
          const topic = process.argv[2];
Neil McPartlin
Neil McPartlin
14,662 Points

'bracket notation' - Thanks Steven. Works a treat.

Line 44: Good spot. We had just learnt about process.argv.slice when looking to capture one or more users in a previous exercise so yes, I ended up trying to restrict slice rather than recognising that it wasn't needed here at all.

I'd just like to take this opportunity to thank you for all the good work you are doing on this forum. I'm sure I'm not alone in appreciating your quality feedback.

Steven Parker
Steven Parker
231,275 Points

:bell: I got alerted by your tag.

It's hard to analyze incomplete code, but I don't see anything obviously wrong with this little portion. Though producing a message like "chalkers has 209 badges and profile.points.JavaScript points in JavaScript" might not be what you're intending .

Perhaps the problem is elsewhere, maybe in the printMessage function?

Always include the complete code, or a link to a repo, or a link to a snapshot of a workspace.

Neil McPartlin
Neil McPartlin
14,662 Points

Thanks Steven. Here is the snapshot. [https://w.trhou.se/hc3nucljht]

The snippet is correct but I was trying to illustrate with console logs that the current entry profile.points.JavaScript is a number, whereas my intended replacement subject is a string, hence this approach does not work, it results in the message you predicted and I understand why.

Currently line 44 of profile.js is the hardcoded working option.