【发布时间】:2025-12-31 05:55:11
【问题描述】:
export const getServerSideProps: GetServerSideProps = async () => {
const ref = collection(db, "books");
const results = [];
const unsub = onSnapshot(ref, (snapshot) => {
snapshot.forEach((doc) => {
results.push({ ...doc.data(), id: doc.id });
});
//here i get the results
console.log(results)
});
// here is empty
console.log(results)
return {
props: {
books: results,
},
};
};
我正在尝试通过 getServerSideProps 函数从 firestore 数据库中获取实时数据,在快照内我可以获得结果,但是当它在数组之外时,它是空的,我无法传递给道具。
【问题讨论】:
-
也许你可以从这个答案中得到一些想法。 *.com/a/51794485/4652307
标签: reactjs firebase google-cloud-firestore next.js server-side-rendering