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 REST APIs with Express Getting to Know REST APIs A Simple API

Vlad Tanasie
Vlad Tanasie
7,201 Points

res.json is not a function

I keep getting this no matter what i do TypeError: res.json is not a function

2 Answers

Steven Parker
Steven Parker
229,644 Points

For more specific answers, always show your code (or provide a link to a repo or snapshot).

But as a generic hint, mostly likely "res" did not get set to an instance of the object that would have a "json" method. I'd guess it's probably either undefined or null at the point where this message is seen. Start by confirming that, and if my guess is right trace back to where "res" should have been assigned and look for the real issue there.

Joseph Butterfield
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Joseph Butterfield
Full Stack JavaScript Techdegree Graduate 16,430 Points

I know this is almost a year later, but I had the same problem. I found the solution was that I posted my get function as

app.get('/greetings', (res, req) => {
    res.json({greeting: "Hello World"})
});

My solution was swapping the res, and req parameters within the callback function. Not sure why that worked, and now I am curious, but it did. ¯_(ツ)_/¯ Solution being, write (req, res) => {}

Can anyone weigh in on why that works?

Steven Parker
Steven Parker
229,644 Points

A response object (the 2nd argument) has a "json" method, but a request object (the first argument) does not.