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 trialTegar Putra
5,820 PointsWhy the badge and javascriptpoint is "Undefined" ?
i wondering why the badge and javascriptpoint is undefined ? anyone can help?
app.js
const http = require('http');
const router = require('./router.js');
//1. Create a web server
const server = http.createServer((req,res)=> {
res.writeHead(200,{'Content-Type':'text/plain'});
router.home(req,res);
router.user(req,res);
})
server.listen(3000, '127.0.0.1', () =>
console.log('Server is running.....'));
router.js
const Profile = require('./profile.js');
function home(req,res){
if(req.url === '/' ){
res.writeHead(200, {'Content-Type':'text/plain'});
res.write('header\n');
res.write('body\n');
res.end('footer\n');
}
}
//rute untuk mencari badges dan.. profil dari yg dicari
function user(req,res){
//if url =='/...'
let username = req.url.replace("/","");
if(username.length > 0){
res.writeHead(200, {'Content-Type':'text/plain'});
res.write('header\n');
//ambil json dari treehouse
const studentProfile = new Profile(username);
//on 'end'
studentProfile.on("end", function(profileJSON){
//tampilkan profil
//simpan value yang dibutuhkan
const values = {
avatarUrl: profileJSON.gravatar_url,
username: profileJSON.profile_name,
badges: profileJSON.badges_length,
javascriptPoints: profileJSON.points_Javascript
}
res.write(values.username + ' memiliki ' + values.badges + ' dan ' + values.javascriptPoints + '\n');
res.end('footer');
})
//on 'error
studentProfile.on("error", function(error){
//show error
res.write(error.message + "\n");
res.end('Footer\n');
})
}
//tampilkan error
}
module.exports.home = home;
module.exports.user = user;
2 Answers
Steven Parker
231,269 PointsYou have a few issues with parsing the response. You need the membership operator (period) instead of underscore when accessing a sub-property:
- instead of
profileJSON.badges_length
, you probably wantprofileJSON.badges.length
- instead of
profileJSON.points_Javascript
tryprofileJSON.points.JavaScript
Kevin Gates
15,053 PointsWhat Steven said. Should be:
var values = {
avatarUrl: profileJSON.gravatar_url,
username: profileJSON.profile_name,
badges: profileJSON.badges.length,
javascriptPoints: profileJSON.points.JavaScript
}
Steven Parker
231,269 PointsSteven Parker
231,269 PointsTegar Putra — were you able to resolve the issue? You can mark a question solved by choosing a "best answer".
Happy coding!