What's the best way to get firestore collection or query data into getServerSideProps with nextJS and react-firebase-hooks

huangapple go评论62阅读模式
英文:

What's the best way to get firestore collection or query data into getServerSideProps with nextJS and react-firebase-hooks

问题

I'm trying to convert an application to NextJS. In particular, I'm working on converting this file where I do some simple client-side data fetches to get a list of posts from my firestore:

// Posts page

const q = query(
    collection(db, "posts"),
    orderBy("likes", "desc")
);

In the child component, I then use the useCollection hook from react-firebase-hooks to take the result of that query and turn it into usable data.

I thought that in NextJS I would just be able to throw all of this into getServerSideProps and then use it that way. However, as firestore returns a bunch of functions rather than just JSON, the following snippet fails:

export async function getServerSideProps() {
  const posts = await query(collection(db, "posts"), orderBy("likes", "desc"));
  return { props: { posts } };
}

So I tried to bring the useCollection hook into my getServerSideProps fn, but that failed because it's not a React component or hook, so I can't use custom hooks.

I'm new to NextJS so I'm not even sure if I'm approaching this the right way. I've looked around for examples of using collections in getServerSideProps, but found nothing. All the examples I've seen are around getting a single doc rather than querying for a whole collection and then passing down that data. (e.g. this one).

How can I get collection data from firebase into my Posts page while still using this hook library?

英文:

I'm trying to convert an application to NextJS. In particular, I'm working on converting this file where I do some simple client-side data fetches to get a list of posts from my firestore:

// Posts page

const q = query(
    collection(db, "posts"),
    orderBy("likes", "desc")
);

In the child component, I then use the useCollection hook from react-firebase-hooks to take the result of that query and turn it into useable data.

I thought that in NextJS I would jut be able to throw all of this into getServerSideProps and then use it that way. However, as firestore returns a bunch of functions rather than just JSON, the following snippet fails:

export async function getServerSideProps() {
  const posts = await query(collection(db, "posts"), orderBy("likes", "desc"));
  return { props: { posts } };
}

So I tried to bring the useCollection hook into my getServerSideProps fn, but that failed because it's not a React component or hook, so I can't use custom hooks.

I'm new to NextJS so I'm not even sure if I'm approaching this the right way. I've looked around for examples of using collections in getServerSideProps, but found nothing. All the examples I've seen are around getting a single doc rather than querying for a whole collection and then passing down that data. (e.g. this one).

How can I get collection data from firebase into my Posts page while still using this hook library?

答案1

得分: 1

"query" 仅用于指定您要从集合或集合组中检索哪些文档。

但您必须使用 "getDocs" 来获取属于特定集合的文档。请查看下面的代码。

import { getDocs, collection, query, orderBy } from "firebase/firestore"

export async function getServerSideProps() {
  const postsRef = query(collection(db, "posts"), orderBy("likes", "desc"))
  const postsSnapshot = await getDocs(postsRef)
  const posts = postsSnapshot.docs.map((doc) => doc.data())
  return { props: { posts } }
}

查看文档以获取更多信息。

英文:

query is only used for specifying which documents you want to retrieve from a collection or collection group.

But you must use getDocs to fetch documents belonging to particular collection. Check the code below.

import { getDocs, collection, query, orderBy } from "firebase/firestore"

export async function getServerSideProps() {

 const postsRef = query(collection(db, "posts"), orderBy("likes", "desc"))

 const postsSnapshot = await getDocs(postsRef)
 const posts = postsSnapshot.docs.map((doc) => doc.data());
 return { props: { posts } };
}

Check documentation for more information.

huangapple
  • 本文由 发表于 2023年4月19日 17:07:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定