route handler error
Answered
English Mastiff posted this in #help-forum
English MastiffOP
hi, everybody.
this is my route handler for alogin endpoint made with django:
import { NextApiRequest, NextApiResponse } from "next";
import axios from "axios";
export async function POST(req: NextApiRequest, res: NextApiResponse) {
// const { email, password } = req.body;
const email = 'saul_rr77@hotmail.com';
const password = 'siuntejis';
console.log(email)
console.log(password)
try {
const response = await axios.post("http://127.0.0.1:8000/users/login/", {
email,
password,
});
return res.json(response.data); // Send successful response
} catch (error:any) {
console.log("error")
const errorMessage = error.response?.data?.message || "Error logging in";
console.log('res: ',res)
return res.status(500).json({ message: errorMessage });
}
}
and it is giving me the following error:
TypeError: res.status is not a function
at POST (webpack-internal:///(rsc)/./src/app/api/login/route.ts:23:20)
thank you in advance for you valuable help
this is my route handler for alogin endpoint made with django:
import { NextApiRequest, NextApiResponse } from "next";
import axios from "axios";
export async function POST(req: NextApiRequest, res: NextApiResponse) {
// const { email, password } = req.body;
const email = 'saul_rr77@hotmail.com';
const password = 'siuntejis';
console.log(email)
console.log(password)
try {
const response = await axios.post("http://127.0.0.1:8000/users/login/", {
email,
password,
});
return res.json(response.data); // Send successful response
} catch (error:any) {
console.log("error")
const errorMessage = error.response?.data?.message || "Error logging in";
console.log('res: ',res)
return res.status(500).json({ message: errorMessage });
}
}
and it is giving me the following error:
TypeError: res.status is not a function
at POST (webpack-internal:///(rsc)/./src/app/api/login/route.ts:23:20)
thank you in advance for you valuable help
Answered by Anay-208
5 Replies
Answer
in route handlers you dont do (req: NextApiRequest, res: NextApiResponse), you do (request: NextRequest) and you return a Response instance
the syntax is completely different from api routes/express. check the link above for details