res.status is not a function
Answered
Black carpenter ant posted this in #help-forum
Black carpenter antOP
What is wrong here in my api call? I've put it in src/app/subscribe/route.js and the request goes trough fine but seems to cause some error...
import axios from "axios";
export const POST = async (req, res) => {
const body = await req.json()
const { email } = body;
const MAILCHIMP_API_KEY = process.env.MAILCHIMP_API_KEY;
const MAILCHIMP_LIST_ID = process.env.MAILCHIMP_LIST_ID;
console.log('MAILCHIMP_API_KEY', MAILCHIMP_API_KEY)
console.log('MAILCHIMP_LIST_ID', MAILCHIMP_LIST_ID)
console.log('email_address', email)
const data = {
email_address: email,
status: "subscribed",
tags: ["signup-landingpage"],
};
try {
const response = await axios.post(
`https://us8.api.mailchimp.com/3.0/lists/${MAILCHIMP_LIST_ID}/members`,
data,
{
auth: {
username: "Test",
password: MAILCHIMP_API_KEY,
},
}
);
// Check the response status code
if (response.status === 200) {
// Success!
res.status(200).json({ status: "Subscribed" });
} else {
// Error
throw new Error(`Mailchimp API error: ${response.statusText}`);
}
} catch (error) {
console.error("Mailchimp Error:", error);
res.status(500).json({ status: "An error occurred. Please try again later." });
}
};10 Replies
Black carpenter antOP
The error:
Mailchimp Error: TypeError: res.status is not a function
at POST (webpack-internal:///(rsc)/./src/app/api/subscribe/route.js:32:17)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:254:37)
- error TypeError: res.status is not a function
at POST (webpack-internal:///(rsc)/./src/app/api/subscribe/route.js:41:13)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async eval (webpack-internal:///(rsc)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:254:37)
- wait compiling...
- event compiled successfully in 48 ms (153 modules)
- error Detected default export in '/Users/alex/Documents/CODE/sapisalary-fe/sapisalary/src/app/api/subscribe/route.js'. Export a named export for each HTTP method instead.
- error No HTTP methods exported in '/Users/alex/Documents/CODE/sapisalary-fe/sapisalary/src/app/api/subscribe/route.js'. Export a named export for each HTTP method.
- error Detected default export in '/Users/alex/Documents/CODE/sapisalary-fe/sapisalary/src/app/api/subscribe/route.js'. Export a named export for each HTTP method instead.
- error No HTTP methods exported in '/Users/alex/Documents/CODE/sapisalary-fe/sapisalary/src/app/api/subscribe/route.js'. Export a named export for each HTTP method.Giant panda
You have that issue due to
res.status(200)Giant panda
That's not how responses are done in route handlers
Answer
Giant panda
In fact the
res parameter doesn't even existYou return
Response objects directly.Please refer to the examples in the docs for that.
Black carpenter antOP
@Giant panda how to check if the response is 200 then?
Giant panda
The check whether the third party API call was successful is completely fine. It's the line after that.
Please read thoroughly what I wrote.
Spectacled Eider
some examples can be found in https://nextjs.org/docs/app/building-your-application/routing/route-handlers