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

JavaScript

matt duBurg
matt duBurg
5,712 Points

Node.js course - getting same error when using template literal username

"undefined:1 <html><body>You are being <a href="https://teamtreehouse.com/home">redirected</a>.</body></html> SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at IncomingMessage.<anonymous> ((removed)/profileSearch/app.js:24:40) at IncomingMessage.emit (node:events:539:35) at endReadableNT (node:internal/streams/readable:1344:12) at process.processTicksAndRejections (node:internal/process/task_queues:82:21)"

I get this error any time i try to run the app and I cant find any resources to help me. when the request was previously not a template literal I was not having this issue, even though the issue appears to be the json that the program is expecting. Has anyone experienced anything like this?

matt duBurg
matt duBurg
5,712 Points

(code posted here)

const https = require("https");
//print the data
function printMessage(username, badge_count, points) {
    const message = `${username} has ${badge_count} total badge(s) and ${points} points in JavaScript`;
    console.log(message);
}

function getProfile(username) {
    try {
        //connect to API URL (https:teamtreehouse.com/mattduburg.json)
        const request = https.get(

            `https://teamtreehouse.com/profiles/${username}.json`,
            (response) => {
                let body = ""
                console.dir(response.statusCode);
                //read the data
                response.on("data", (data) => {
                    //parse the data
                    body += data.toString();
                });
                response.on("end", () => {
                    //parse the data
                    let profile = JSON.parse(body)
                    printMessage(username, profile.badges.length, profile.points)
                    console.dir(JSON.parse(body));
                })
            });
    } catch (error) {
        console.error(error.message);
    }

}
getProfile();

1 Answer

Mark Sebeck
MOD
Mark Sebeck
Treehouse Moderator 37,901 Points

Hi Matt. Looks like you forgot to pass in a username. When I run your code with

getProfile("marksebeck");

it runs fine