Currently working on a Next.js project that involves MongoDB integration. I am using the app router to test API calls with the code below, and surprisingly, I am receiving a response from the database.
import { NextApiRequest, NextApiResponse, NextApiHandler } from "next";
import User from "../../model/User";
import clientPromise from "../../lib/mongodb";
// Handling GET method
export const GET: NextApiHandler = async (req, res) => {
await clientPromise;
const allUsers = await User.find({})
.then((data) => {
console.log(data);
return res.status(200).json(data);
})
.catch((err) => {
console.log(err);
return res.status(500).json({ error: err.toString() });
});
};
TypeError: res.status is not a function`
Despite trying to log it to the console, I can't seem to resolve this error. The database is functioning properly, but I keep encountering a 500 server error.
I attempted a slightly different approach as shown below:
export async function GET(req: NextApiRequest, res: NextApiResponse) {
await clientPromise;
const allUsers = await User.find({})
.then((data) => {
console.log(data);
return res.status(200).json(data);
})
.catch((err) => {
console.log(err);
return res.status(500).json({ error: err.toString() });
});
}