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
Eric Park
1,063 PointsWhy is response not defined?
I am running node app.js, but I keep getting an error:
ReferenceError: response is not defined
at Object.<anonymous> (/home/treehouse/workspace/app
.js:21:1)
at Module._compile (module.js:410:26)
at Object.Module._extensions..js (module.js:417:10)
at Module.load (module.js:344:32)
at Function.Module._load (module.js:301:12)
at Function.Module.runMain (module.js:442:10)
at startup (node.js:136:18)
at node.js:966:3
Here is my actual code:
//Problem: We need a simple way to look at user's badge count and Javascript points
//Solution: Use node.js to connect to Treehouse's API to get profile information to print out
var https = require("https");
var username = 'ericpark';
function PrintMessage(username, badgeCount, points) {
var message = username + " has " + badgeCount + " total badges and " + points + " total points in Javascript.";
console.log(message);
}
//Connect to the API URL (http://teamtreeouse.com/username.json)
var request = https.get("https://teamtreehouse.com/" + username +".json", function(response){
console.log(response.statusCode);
});
request.on("error", function(error){
console.error(error.message);
});
//Read the data
response.on('data', function (chunk) {
console.log('BODY: ' + chunk)
});
//Parse the data
//Print the data out
3 Answers
grahammowat2
6,038 PointsYou have made one simple mistake took me a while to notice it myself. You have put the response on it's own i.e.
response.on('data', function(chunk){
console.log('BODY: ' + chunk);
});
Where you should actually have this response inside your request like so:
var request = https.get("https://teamtreehouse.com/" + username + ".json", function(response){
console.log(response.statusCode);
response.on('data', function (chunk) {
console.log('BODY: ' + chunk)
});
});
I hope this makes sense for you. Response is not defined for because you have declared the response outside of your https.get function.
Steven Parker
243,215 Points On line 21, you call response.on at the outer level, but you haven't defined response in the outer context. It's only defined within the context of the anonymous function inside the https.get.
There's a similar call to request.on at line 16, but that was already defined on line 12.
You also seem to have had a blockquoting issue with quote marks, but I'm assuming that's not the issue.
Eric Park
1,063 PointsCoolio thanks!