如何在Firestore中使用子集合的orderBy

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

How to use orderBy subcollection in firestore

问题

以下是您要翻译的内容:

I want to sort data by timestamp in firestore subcollection using orderBy. I looked example and tried to follow but it didn't work. How to use orderBy timestamp in firestore with React. This is my reference to fetch data in subcollection ref

this is my result on web result

App.js

import "./App.css";
import { collection } from "@firebase/firestore";
import { useCollectionData } from "react-firebase-hooks/firestore";
import { db } from "./firebase";
import ChildrenList from "./ChildrenList";

export default function App() {
  const query = collection(db, "profile");
  const [docs, loading, error] = useCollectionData(query);

  return (
    <div>
      <h1>test</h1>

      {loading && "Loading..."} 

        <ul>
          <tbody>
            {docs?.map((doc) => (    
          <div key={Math.random()}>
            <ChildrenList  path={`profile/${doc.uid}/time`} />
          </div>
        ))}
        </tbody>
      </ul>
    </div>
    
  );
}

ChildrenList.js

import { collection } from "@firebase/firestore";
import { useCollectionData } from "react-firebase-hooks/firestore";
import { db } from "./firebase";
import Table from 'react-bootstrap/Table';

export default function ChildrenList({ path }) {
  const query = collection(db, path);
  const [docs, loading, error] = useCollectionData(query);

  return (
    <ul>
      {loading && "Loading..."}
      <Table striped bordered hover variant="dark" size="md">
        <tbody> 
           {docs?.map((doc) => {
            const timestamp = { nanoseconds: doc.timeStamp.nanoseconds, seconds: doc.timeStamp.seconds }
            const firebasetime = new Date(
              timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000,
            );
            const date = firebasetime.toDateString();
            const timeStamp = firebasetime.toLocaleTimeString();
            console.log(date, timeStamp);
            return(
            <tr>
              <td>{doc.name}</td>
              <td>{doc.sid}</td>
              <td>{doc.group}</td>
              <td>{doc.room}</td>              
              <th>{date}</th>
              <th>{timeStamp}</th>
            
              </tr>
            
           )})}</tbody>
           </Table>
      
    </ul>
  );
}

希望这对您有所帮助。如果您需要进一步的协助,请随时告诉我。

英文:

I want to sort data by timestamp in firestore subcollection using orderBy. I looked example and tried to follow but it didn't work. How to use orderBy timestamp in firestore with React. This is a my reference to fetch a data in subcollection ref

this is a my result on web
result

App.js

import &quot;./App.css&quot;;
import { collection } from &quot;@firebase/firestore&quot;;
import { useCollectionData } from &quot;react-firebase-hooks/firestore&quot;;
import { db } from &quot;./firebase&quot;;
import ChildrenList from &quot;./ChildrenList&quot;;


export default function App() {
  const query = collection(db, &quot;profile&quot;);
  const [docs, loading, error] = useCollectionData(query);

  return (
    &lt;div&gt;
      &lt;h1&gt;test&lt;/h1&gt;

      {loading &amp;&amp; &quot;Loading...&quot;} 

        &lt;ul&gt;
          &lt;tbody&gt;
            {docs?.map((doc) =&gt; (    
          &lt;div key={Math.random()}&gt;
            &lt;ChildrenList  path={`profile/${doc.uid}/time`} /&gt;
          &lt;/div&gt;
        ))}
        &lt;/tbody&gt;
      &lt;/ul&gt;
    &lt;/div&gt;
    
  );
}

ChildrenList.js

import { collection } from &quot;@firebase/firestore&quot;;
import { useCollectionData } from &quot;react-firebase-hooks/firestore&quot;;
import { db } from &quot;./firebase&quot;;
import Table from &#39;react-bootstrap/Table&#39;;

export default function ChildrenList({ path }) {
  const query = collection(db, path);
  const [docs, loading, error] = useCollectionData(query);

  return (
    &lt;ul&gt;
      {loading &amp;&amp; &quot;Loading...&quot;}
      &lt;Table striped bordered hover variant=&quot;dark&quot; size=&quot;md&quot;&gt;
        &lt;tbody&gt; 
           {docs?.map((doc) =&gt; {
            const timestamp = { nanoseconds: doc.timeStamp.nanoseconds, seconds: doc.timeStamp.seconds }
            const firebasetime = new Date(
              timestamp.seconds * 1000 + timestamp.nanoseconds / 1000000,
            );
            const date = firebasetime.toDateString();
            const timeStamp = firebasetime.toLocaleTimeString();
            console.log(date, timeStamp);
            return(
            &lt;tr&gt;
              &lt;td&gt;{doc.name}&lt;/td&gt;
              &lt;td&gt;{doc.sid}&lt;/td&gt;
              &lt;td&gt;{doc.group}&lt;/td&gt;
              &lt;td&gt;{doc.room}&lt;/td&gt;              
              &lt;th&gt;{date}&lt;/th&gt;
              &lt;th&gt;{timeStamp}&lt;/th&gt;
            
              &lt;/tr&gt;
            
           )})}&lt;/tbody&gt;
           &lt;/Table&gt;
      
    &lt;/ul&gt;
  );
}

答案1

得分: 0

import { getFirestore, collectionGroup, getDocs,
         query, orderBy, limit } from '@firebase/firestore';

export default function App() {
  // const query = collection(db, "profile"); // This is not query it's reference
  const q = query(collectionGroup(getFirestore(), 'subCollectionName'),
                  orderBy('createdAt', 'desc'), limit(15))
  // I dont know how reactFirebaseHooks returns so make remplate on your own.
  const [docs, loading, error] = useCollectionData(q);
  // Firebase would use `onSnapshoot()` or `getDocs()` function to
  // get data using query i made above.

  return (
    <div>
      <h1>test</h1>
      {loading && "Loading..."} 
        <ul>
          {docs?.map((doc) => (    
            ...
          ))}
        </ul>
    </div>
  );
}

<tbody> tag is for <table> not <ul>. I don't know what react firebase hooks returns, but I know that getDocs from Firebase returns an array of document snapshots that have document data under .data().

英文:
import { getFirestore, collectionGroup, getDocs,
         query, orderBy, limit } from &#39;@firebase/firestore&#39;;

export default function App() {
  // const query = collection(db, &quot;profile&quot;); // This is not query it&#39;s reference
  const q = query(collectionGroup(getFirestore(), &#39;subCollectionName&#39;),
                  orderBy(&#39;createdAt&#39;, &#39;desc&#39;), limit(15))
  // I dont know how reactFirebaseHooks returns so make remplate on your own.
  const [docs, loading, error] = useCollectionData(q);
  // Firebase would use `onSnapshoot()` or `getDocs()` function to
  // get data using query i made above.

  return (
    &lt;div&gt;
      &lt;h1&gt;test&lt;/h1&gt;
      {loading &amp;&amp; &quot;Loading...&quot;} 
        &lt;ul&gt;
          {docs?.map((doc) =&gt; (    
            ...
          ))}
        &lt;/ul&gt;
    &lt;/div&gt;
  );
}

&lt;tbody&gt; tag is for &lt;table&gt; not &lt;ul&gt;. I don't know what react firebase hooks returns, but I know that getDocs from Firebase returns array of documents snapshots that under .data() has document data.

huangapple
  • 本文由 发表于 2023年2月18日 19:22:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75492988.html
匿名

发表评论

匿名网友

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

确定