Firebase .where() .limit() .orderBy() 的新命名空间

发布于 2025-01-15 05:20:54 字数 759 浏览 9 评论 0原文

为了限制对 FirestoreDB 的获取,我尝试使用最近的命名空间来 getDocs()。

const firebaseColRef = collection(db, "collection")

这就是我尝试过的:

const firebaseColRef = collection(db, "collection").limit(x)

这:

const getDB = async () => {
    const data = await getDocs(firebaseColRef).limit(x);
    setFilmDB(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
  }

在这两种情况下,控制台都会提示:

Uncaught (in promise) TypeError: firebase_firestore__WEBPACK_IMPORTED_MODULE_1__.collection(...).limit is not a function

我浏览了几个文档、教程和文章,但无法找到除上述名称空间之外的任何其他名称空间。并尝试任何可能的 .limit(x) 附加以获得除控制台提示符之外的任何其他结果。什么都没起作用。

我缺少什么?

有人能指出我正确的方向吗?

将不胜感激。 谢谢,安东尼

to limit the fetch to FirestoreDB I tried to use the recent namespace to getDocs().

const firebaseColRef = collection(db, "collection")

This is what I tried:

const firebaseColRef = collection(db, "collection").limit(x)

And this:

const getDB = async () => {
    const data = await getDocs(firebaseColRef).limit(x);
    setFilmDB(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })));
  }

In both cases the console prompts:

Uncaught (in promise) TypeError: firebase_firestore__WEBPACK_IMPORTED_MODULE_1__.collection(...).limit is not a function

I went through several documentations, tutorials and articels and was not able to find any other namespace then the above mentioned. And tried any possible append of .limit(x) to get any other result than the console prompt. Nothing worked.

What am I missing ?

Can anyone point me in the right direction ?

Would be very appreciated.
Thanks, Anthony

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2025-01-22 05:20:54

几乎每个 Firebase 方法都是 Modular SDK 中的顶级函数,可以直接从相关 SDK 导入。尝试重构代码,如下所示。

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

const getDB = async () => {
  const colRef = collection(db, "collection");

  // Keep adding query clauses in query()
  // like chaining them in name-spaced V8 version
  const q = query(colRef, where("field", "==", "value"), limit(1));

  const data = await getDocs(q);
  console.log(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })))
}

同一查询的名称空间版本供参考:

const q = db.collection("col").where("field", "==", "value").limit(1);

您可以在 文档

另请查看:

Almost every Firebase method is a top-level function in Modular SDK and can be imported directly from the relevant SDKs. Try refactoring the code as shown below.

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

const getDB = async () => {
  const colRef = collection(db, "collection");

  // Keep adding query clauses in query()
  // like chaining them in name-spaced V8 version
  const q = query(colRef, where("field", "==", "value"), limit(1));

  const data = await getDocs(q);
  console.log(data.docs.map((doc) => ({ ...doc.data(), id: doc.id })))
}

The name-spaced version of the same query for reference:

const q = db.collection("col").where("field", "==", "value").limit(1);

You can find list of all functions in the new Firestore SDK in the documentation.

Also checkout:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文