Facing problem with getServerSideProps in data fetching
Unanswered
Northern Inuit Dog posted this in #help-forum
Northern Inuit DogOP
When I am using
But when I am using
I don't want to use
getBlogs() it's returning array of object in consolefor blogsasync function getBlogs() {
const blogs = await Blog.find();
return blogs;
}But when I am using
getServerSideProps() it's returning empty in console for dataexport async function getServerSideProps() {
const data = await Blog.find();
console.log(data);
return {
props: data,
};
}I don't want to use
getBlogs() because It working properly in localhost but It also showing deleted data when I deploy in vercelor netlify. Is there any method so that every time the component will be re rendered it will show rela time data?14 Replies
the getServerSideProps Function is a Function for the pages router. You marked your post as app router. So which router are you currently using? @Northern Inuit Dog
@B33fb0n3 the getServerSideProps Function is a Function for the pages router. You marked your post as app router. So which router are you currently using? <@513609532921085972>
Northern Inuit DogOP
I am using app router. if I use getBlog(), my code is working fine in localhost but after deploy in vercel or netlify these are also showing deleted data from database.
@Northern Inuit Dog I am using app router. if I use getBlog(), my code is working fine in localhost but after deploy in vercel or netlify these are also showing deleted data from database.
are you hitting the data cache?
You should normally invalidate the specific part, to not have stale data inside your data cache. Are you also doing this, if hitting?
You should normally invalidate the specific part, to not have stale data inside your data cache. Are you also doing this, if hitting?
Northern Inuit DogOP
@B33fb0n3 I didn't don anything with data cache, if I get data from Model of database I don't know how to handle the cache.
@Northern Inuit Dog <@301376057326567425> I didn't don anything with data cache, if I get data from Model of database I don't know how to handle the cache.
you are getting the data from something from your server related? Like own api routes, public folder, ...?
Or external, like third party?
Or external, like third party?
@B33fb0n3 you are getting the data from something from your server related? Like own api routes, public folder, ...?
Or external, like third party?
Northern Inuit DogOP
api route of server
@Northern Inuit Dog api route of server
ok, then it's automatically inside the data cache. Because you are not invalidate it, there is stale data. You can invalidate your data, so no stale data will be shown. Here you can see how to revalidate your data: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data
@B33fb0n3 ok, then it's automatically inside the data cache. Because you are not invalidate it, there is stale data. You can invalidate your data, so no stale data will be shown. Here you can see how to revalidate your data: https://nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data
Northern Inuit DogOP
@B33fb0n3 I am getting data using await Blog.find(), in this source it's getting data fetch(url), is there any way to validate data without fetching it? as I am easily getting data using my Blog model.
yea, you can still fetch data with the fetch Method. The page I just linked you show's different methods how to revalidate data. One is the timebased: (see screenshot)
Take a look at my screenshot and the page I linked you
Take a look at my screenshot and the page I linked you
@Northern Inuit Dog fixed?
@B33fb0n3 <@513609532921085972> fixed?
Northern Inuit DogOP
May be I am unable to understand my problem. I don't want to fetch data. I want to call from database directly without fetching api endpoint. As I am directly calling it from database. It also showing deleted data. It seems every time I am visiting the page it not getting new call from data. But it works fine in localhost.
My database collection name in Blog. I am using mongoose. So I will get all data using
const data = await Blog.find()
If I get data without fetching API url. Because after fetching api endpoint I am getting same result. How can I handle cache of this or send new request every time I visit the page.
My database collection name in Blog. I am using mongoose. So I will get all data using
const data = await Blog.find()
If I get data without fetching API url. Because after fetching api endpoint I am getting same result. How can I handle cache of this or send new request every time I visit the page.
yea, request are valid once on localhost, but they will be cached when using them in production. So if you are able to revalidate your data, you can get exactly that:
send new request every time I visit the page.
@Northern Inuit Dog solved?
@Northern Inuit Dog ?