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 trialCory Carson
16,902 PointsUndefined on local machine
hello, I get the following error when tring to make the JSON call on my local computer (not in Workspaces)
SyntaxError: Unexpected token u in JSON at position 0
Here is the code:
const https = require('https');
const username = "chalkers";
//1. find the REST api on the website url here:
// treehouse.com/corycarson.json
//2. read the data
//3. parse the data
//4. print out the data
//two types of events
// system events: setTimeout()
// user events: mouseClick()
//1. function to print site to console
function printMessage(username, badgeCount, points) {
const message = `${username} has ${badgeCount} total badge(s) and ${points} in Javascript`;
console.log(message);
}
//console test of function
//printMessage("cory", 100, 2000000);
//
//2. use httpS get method
//2.1connect to the API URL
//2.2 initiate callback request using an arrow fx
const request = https.get(`https://teamtreehouse.com/${username}.json`, response => {
console.dir(response.status)
//2.5 concatenate the buffer to a variable
let body = "";
//read the data
response.on('data', data => {
//2.4 output the buffer data to a string format
body += console.log('data:', data.toString()
);
//response is sent in a buffer
//node.js does not wait until the buffer returns in order to read the data
//this is why it is a non-blocking language
//the .toString()
// we can concatenate the buffer by attaching it to a variable
});
//2.6 implement an END event on the response
response.on('end', () => {
//3. parsing data: turn string to object
const profile = JSON.parse(body);
printMessage(username, profile.badges.length, profile.points.javascript)
console.dir(profile);
//2.7 read out the body variable containing the buffer
console.log(body);
//currently the 'body' variable is a string, we want an object
//to determine the type of an object
// console.log(typeof body)
});
});
2 Answers
Cory Carson
16,902 Pointsthe problem was all the 'console.log' functions hadn't been removed; this was returning an undefined message
Rich Zimmerman
24,063 Pointsright but what i'm saying is you're calling a function that does not return a value into a variable. variables are typically used to store values. So you're using a variable to essentially store nothing, that is why it's undefined.
you probably instead want it to be
body += data.toString();
// rather than
body += console.log('data:', data.toString() );
this way it's storing a string in 'body'.
Cory Carson
16,902 Pointsthanks rich, we responded at the same time so my response was to myself rather than your revision; appreciate the help
Rich Zimmerman
24,063 PointsRich Zimmerman
24,063 Pointsit's hard to read the code but i did see this..
body += console.log('data:', data.toString() );
you're making assigning a console.log to a variable. which will make it undefined and therefore unable to parse.