Rupey_N_A
(Rupey N/A)
1
I am following this tutorial:
My code is:
import clientPromise from "../lib/mongodb";
export default async (req, res) => {
try {
const client = await clientPromise;
const db = client.db("Reports");
const collection = await db
.collection("Reports")
const report = await db
.collection("Reports")
.find({})
.toArray()
res.json(report);
} catch (e) {
console.error(e);
}
};
In my code the report after .toArray() returns my data within an array. However, I get this error:
TypeError: Cannot read properties of undefined (reading 'json')
It seems the data comes through to the array but then there is an issue returning it as json.
rasaq_adewuyi
(Abdulrasaq Jamiu Adewuyi)
2
NextJS made an update on this, you should return your response like so:
return new Response(JSON.stringify({report : report}), { status: 200 })
Update your code as below, it should work.
import clientPromise from "../lib/mongodb";
export const GET = async (res, req) =>{
try {
const client = await clientPromise;
const db = client.db("Reports");
const collection = await db
.collection("Reports")
const report = await db
.collection("Reports")
.find({})
.toArray()
return new Response(JSON.stringify({report : report}), { status: 200 })
} catch (e) {
console.error(e);
}
};
Let me know if this is helpful.