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 trialErik L
Full Stack JavaScript Techdegree Graduate 19,470 Pointsnode.js module.exports??
Hi I just finished watching this video, https://teamtreehouse.com/library/organizing-your-code-with-require, in this video we are tasked with using module.exports.get = get to extract 'data', however my console is showing lots of errors this is what I have, the following file is called profile.js
//Require https module
const https = require('https')
//Require https module FOR STATUS CODE
const http = require('http')
//Print error messages
function printError(error) {
console.error(error.message);
};
//Function to print message to console
function printMessage(username, badgeCount, points) {
const message = `${username} has ${badgeCount} total badge(s) and ${points} points in Javascript`;
console.log(message);
}
function get(username) {
try {
//Connect to the API URL (https://teamtreehouse.com/username.json)
const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {
console.log('the status code is ' + response.statusCode);
//Handle responses that are not 200
if (response.statusCode === 200) {
let body = '';
//Read the data
response.on('data', data => {
body += data.toString();
});
response.on('end', () => {
try {
//Parse the data
const profile = JSON.parse(body);
printMessage(username, profile.badges.length,
profile.points.JavaScript);
} catch (error) {
printError(error);
}
//Print the data
});
} else {
const message = `There was an error getting the profile for ${username} (${http.STATUS_CODES[response.statusCode]})`;
const statusCodeError = new Error(message);
printError(statusCodeError);
}
});
//this object has an error object passed into it
request.on('error',printError);
} catch (error) {
printError(error);
}
};
module.exports.get = get;
this file is called Project.js
// 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
//Require https module
const https = require('https')
//Require https module FOR STATUS CODE
const http = require('http')
//Print error messages
function printError(error) {
console.error(error.message);
};
//Function to print message to console
function printMessage(username, badgeCount, points) {
const message = `${username} has ${badgeCount} total badge(s) and ${points} points in Javascript`;
console.log(message);
}
function getProfile(username) {
try {
//Connect to the API URL (https://teamtreehouse.com/username.json)
const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {
console.log('the status code is ' + response.statusCode);
//Handle responses that are not 200
if (response.statusCode === 200) {
let body = '';
//Read the data
response.on('data', data => {
body += data.toString();
});
response.on('end', () => {
try {
//Parse the data
const profile = JSON.parse(body);
printMessage(username, profile.badges.length,
profile.points.JavaScript);
} catch (error) {
printError(error);
}
//Print the data
});
} else {
const message = `There was an error getting the profile for ${username} (${http.STATUS_CODES[response.statusCode]})`;
const statusCodeError = new Error(message);
printError(statusCodeError);
}
});
//this object has an error object passed into it
request.on('error',printError);
} catch (error) {
printError(error);
}
};
/*
ForEach iterates over the array and passes each member into a callback
function, each member would be a username
*/
console.log(process.argv);
const users = ["chalkers","alenaholligan", "davemcfarland"];
users.forEach((username) => {
getProfile(username);
});
and lastly, this file is called app.js
const profile = require ('./profile,js');
const users = process.argv.slice(2);
users.forEach(profile.get);
// console.log("Hello World!");
// console.error("Oops something went wrong");
// console.dir({name: "Andrew", age: 33 }); //when passing in object,dir prints out the values and keys in a human readable format
can someone please help?
1 Answer
Liam Clarke
19,938 PointsHi Erik
Before i run this code, theres a few potential solutions that i can see scanning through your code, the main one that stands out to me is the comma.
The first line of your app.js, you use a comma(,) instead of a full stop (.) on profile.js
const profile = require ('./profile,js');
If this doesn't fix your problem can you paste your error message please