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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![Tegar Putra](https://uploads.teamtreehouse.com/production/profile-photos/10608655/micro_azaysyariah___Bga2SaWF_1k___.jpg)
Tegar 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](https://uploads.teamtreehouse.com/production/profile-photos/969492/micro_tool_chess_set_2.jpg)
Steven Parker
230,688 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](https://secure.gravatar.com/avatar/e6d902128dd45e6d463bc4f42dee5fcd?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
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
230,688 PointsSteven Parker
230,688 PointsTegar Putra — were you able to resolve the issue? You can mark a question solved by choosing a "best answer".
Happy coding!