Are you stuck to this “typeerror: res.json is not a function” error message while doing your program?
And so, continue reading to fix this error.
In this article, we’ll hand you the solutions for “res.json is not a function.”
But before that, we’ll give you first a better understanding of this error.
What is “typeerror: res.json is not a function”?
The “typeerror: res.json is not a function” is an error message that can occur when working with Node.js applications and Express.
This error message indicates that the res.json() method, used to send JSON responses to clients, is not recognized as a function by the application.
For example:
const express = require('express');
const app = express();
app.get('/json', (res, req) => {
res.json({ "message": "Hello json" });
});
app.listen(3000);
As you can see in our example, the arguments in the callback function are in the wrong order.
The first argument should be req (request), and the second should be res (response).
As a result, it will throw a “typeerror: res.json is not a function” error message.
Why does this error occur?
This error can occur for several reasons, such as:
❌ Overwriting the res variable in the code.
❌ Having the arguments in the handler method in the wrong order.
❌ Including version mismatches between dependencies.
❌ Missing middleware configuration
❌ Incorrect function calls
❌ Outdated Node.js versions.
How to fix “typeerror: res.json is not a function”?
Now that you fully understand why this error occurs in your code.
Let’s jump into the solutions to resolve this error.
Solution 1: Check the order of the arguments in the callback function
Ensure that the first argument is req (request) and the second is res (response).
For example:
app.get('/json', (res, req) => {
res.json({ "message": "Hello json" });
});
To fix this error, you have to switch the order of the arguments.
const express = require('express');
const app = express();
app.get('/json', (req, res) => {
res.json({ "message": "Hello json" });
});
app.listen(3000);
Solution 2: Avoid overwriting the res variable
Ensure that you are not overwriting the res variable in a callback function.
For example:
app.post('/danger', function response(req, res) {
let placeId = req.body.data;
let option = {
uri: 'https://maps.googleapis.com/maps/api/directions/json?',
qs: {
origin: `place_id:${placeId[0]}`,
destination: `place_id:${placeId[1]}`,
language: 'en',
mode: 'running',
alternatives: true,
key: APIKey
}
};
rp(option)
.then(function (response) { //change the variable name of "res" to "response"
let dangerRate = dangerTest(JSON.parse(response), riskGrid);
res.json({ data: [response, dangerRate] });
})
.catch(function (err) {
console.error("Failed to get JSON from Google API", err);
})
});
Solution 3: Ensure that you have included the required dependencies
Ensure that you have included all the required dependencies such as Express
For example:
const express = require('express');
const app = express();
Solution 4: Verify if you are using a library that makes .json a function
Unless you are using a library that makes .json a function, JavaScript uses JSON with two methods .parse() and .stringify().
When you are trying to set an object property by the name of .json.
res.json = { data: [res, dangerRate] };
Solution 5: Verify if you are using the new httpClient library
With the new httpClient library, you don’t have to call the .json() method.
You can use this simple map instead of the json method:
.map(res => res);
Difference between “res.json is not a function” and “responce.json is not a function”?
Both res.json is not a function and responce.json is not a function are error messages that may occur in the context of web development.
If you are confused about either of these two is the same, well the answer is “No.”
Response.json and res.json are not the same.
Response.json | res.json |
---|---|
response.json is a method on the Response object in the Fetch API that reads the response and returns a promise that resolves with the result of parsing the body text as JSON. | Res.json is a method on the res object in Express.js that sends a JSON response. |
Both indicate that the json method is not recognized as a function of the response or res object, respectively.
The difference between the two error messages is the name of the variable used to reference the response object.
Conclusion
The “typeerror: res.json is not a function” is an error message that can occur when working with Node.js applications and Express.
This article already provides several solutions above so that you can fix the error message immediately.
We are hoping that this article provided you with sufficient solutions to get rid of the error.
You could also check out other “typeerror” articles that may help you in the future if you encounter them.