Data Fetching from MongoDB
Answered
readme posted this in #help-forum
readmeOP
Im using Next.js 14.0.3 for my full stack project where in im using the next hs in built api for data fetching from my MongoDB database. In development when I perform a crud operation ii get redirected to the Home Screen and I see the new entry right away. But in production when I perform a crud operation the database gets updated but I cannot see the data on the home page. I’ll provide the link to my website and GitHub.
GitHub: https://github.com/acesFatee/promptify
Link: https://promptify-olive.vercel.app
GitHub: https://github.com/acesFatee/promptify
Link: https://promptify-olive.vercel.app
64 Replies
it is because the endpoint
/api/prompt/get is statictry set it dynamic by
export const dynamic = "force-dynamic" in the route.ts filethere is a report after you run
next build. You should see that route is static generatedreadmeOP
This is what i got when i build it without adding the dynamic const. As you can see the api/prompt/get is dynamic
import { connectToMongo } from "@/utils/db";
import Prompt from "@/utils/models/PromptModel";
export const GET = async (req, res) => {
try {
await connectToMongo();
const allPrompts = await Prompt.find().populate('creator')
return new Response(JSON.stringify(allPrompts), { status: 200 });
} catch (error) {
return new Response("Failed to fetch prompts", { status: 500 });
}
}; and this is my route.js @Rayok which page of data is not getting updated?
readmeOP
just the home page. I can see the new data on my profile or when i click the tag. but i does not update on the home page
the home page is static generated
either make it dynamic or ISR
oh wait, you doing client side fetching on home page right?
readmeOP
yes
how do you update the data? by
/api/prompt/new endpoint?readmeOP
by the /api/prompt/update endpoint
i have it setup
import { connectToMongo } from "@/utils/db";
import Prompt from "@/utils/models/PromptModel";
export const PATCH = async (req, { params }) => {
const { prompt, tag } = await req.json();
try {
await connectToMongo();
const editedNote = await Prompt.findByIdAndUpdate(params.id,{
prompt: prompt,
tag: tag
})
return new Response(JSON.stringify({editedNote}), { status: 200 });
} catch (error) {
return new Response("Error Updating Prompt", { status: 500 });
}
};so /api/prompt/update/[id]/
import { connectToMongo } from "@/utils/db";
import Prompt from "@/utils/models/PromptModel";
export const PATCH = async (req, { params }) => {
const { prompt, tag } = await req.json();
try {
await connectToMongo();
const editedNote = await Prompt.findByIdAndUpdate(params.id,{
prompt: prompt,
tag: tag
})
revalidatePath("/api/prompt/get")
return new Response(JSON.stringify({editedNote}), { status: 200 });
} catch (error) {
return new Response("Error Updating Prompt", { status: 500 });
}
};can you try add
revalidatePath("/api/prompt/get")?to /api/prompt/update
readmeOP
ok so when i make this change am i supposed to redeploy the app or it will automatically update on the commit
need redeploy
readmeOP
ok ill test this in dev and then redeploy
if you connected the project to vercel, it should automatic redeploy on vercel when you commit the change on github
you can test it on local by
next build & next startreadmeOP
alright
so it gives me an error when i revalidate the api route. Should i instead revalidate the / route because that's the one that is rendering?
oh well
ok remove it and try put
export const dynamic = "force-dynamic" to app/api/prompt/get/route.tsreadmeOP
alright
import { connectToMongo } from "@/utils/db";
import Prompt from "@/utils/models/PromptModel";
export const dynamic = "force-dynamic"
export const GET = async (req, res) => {
try {
await connectToMongo();
const allPrompts = await Prompt.find().populate('creator')
return new Response(JSON.stringify(allPrompts), { status: 200 });
} catch (error) {
return new Response("Failed to fetch prompts", { status: 500 });
}
}; like this>?
yes
readmeOP
ok
I think it works now?
https://promptify-olive.vercel.app/api/prompt/get is not cached anymore
readmeOP
thank you bro it worked
ill test it a couple of times and let you know if there are any bugs
thats the new link
check all api route
readmeOP
yes on it
hmm get only
readmeOP
yes all the crud functionalities work
https://promptify-olive.vercel.app/api/prompt/get this was return HIT
readmeOP
where do you check that
dev console
network tab
readmeOP
i got this
yea this fine
readmeOP
alright thank bro
if you remove
export const dynamic = "force-dynamic", it should be HITyou can try it
readmeOP
but then why was it working fine in development
it didnt have export const dynamic = 'force-dynamic' in dev
because it will only be generated the page when you build it
all are dynamic in development
readmeOP
understood
readmeOP
thank you
np 😀
please mark solution if you have fixed your issue
Answer