working on a nextjs project using mongodb. Using app router, trying to test api calls using the following code, weirdly enough I get a response from the db
import { NextApiRequest, NextApiResponse ,NextApiHandler } from "next";
import User from "../../model/User";
import clientPromise from "../../lib/mongodb";
// Handle 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
I have tried to console log it but the error is never cleared. DB is happy but just keep getting 500 server error
I have tried doing this slightly different as 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()
});
});
}
New contributor
Daniel is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.