当我使用Next.js和revalidate方法/getServerSideProps时,我的结果只包含20个数据,尽管有112个。如果我将整个内容更改为“无存储”,则返回所有112个数据。我使用App Router方法。
奇怪的是,在调用mongoose的GET方法中,我可以看到所有的记录。但是,action.js中只有20个
app/Products/route.js:
export async function GET(request) {
try {
await mongoConection();
} catch (error) {
console.log(error);
}
const products = await Product.find({}).sort({
createdAt: -1,
});
//Products length = 112
console.log("Products length" + products.length);
return NextResponse.json({ products });
}
app/FetchProducts/action.js:
"use server";
export async function FetchProducts() {
const result = await fetch(`${process.env.NEXT_PUBLIC_URL}/Products`, {
next: { revalidate: 3 },
});
const expected = await result.json();
//expectedlength = 20
console.log("expected length: " + expected.products.length);
return expected;
}
app/FetchProducts/page.js:
const fetchProducts = async () => {
setLoadMore(true);
const fetching = await FetchProducts();
//ONLY 20 Result
setResult(fetching.products);
setLoadMore(false);
};
1条答案
按热度按时间6uxekuva1#
从这里
任何不直接使用fetch的数据获取库都不会影响路由的缓存,并且根据路由段的不同,数据获取库将是静态的还是动态的
在
GET
路由中,您没有使用fetch
。这就是为什么你会得到所有的结果。但在操作中,您使用fetch
,并且可能您的第一个fetch
请求仅返回20,并且这些都被缓存。