简体   繁体   English

如何在 Firestore v9 中访问数组

[英]How to access array in Firestore v9

Articles I've read/tried before:我以前读过/尝试过的文章:

I have a Firestore that looks like this我有一个看起来像这样的 Firestore 在此处输入图像描述

  1. The current feature I'm trying to implement is a block list.我正在尝试实现的当前功能是阻止列表。 Whenever you go to the person's profile, you click block, and the person doing the blocking's UID is stored as the doc id and the blocked user's id is added to the "blockedUserId" array每当您 go 到此人的个人资料时,您单击阻止,执行阻止的 UID 的人将存储为文档 ID,并将被阻止的用户 ID 添加到“blockedUserId”数组
  2. The home page displays all the posts and I've been trying to filter the displayed posts to not include posts from any of the users in that "blockedUserId" array主页显示所有帖子,我一直在尝试过滤显示的帖子以不包括来自“blockedUserId”数组中任何用户的帖子
  3. How do I go about doing this correctly?如何正确执行此操作?

Here is the attempted code这是尝试的代码

query(
      collection(getFirestore(fireApp), "posts"),
      orderBy("uid"),
      where(
        "uid",
        "not-in",
        doc(getFirestore(fireApp), "block", "vXLCRjlhOVW6oFOJvtmML6OolKA2")
      )

Firestore queries can only filter on values in the document itself, and values you explicitly pass in to the query. Firestore 查询只能过滤文档本身中的值,以及您明确传递给查询的值。 Your doc(getFirestore(fireApp), "block", "vXLCRjlhOVW6oFOJvtmML6OolKA2") creates a DocumentReference , so the query returns documents from posts that don't contain that document reference.您的doc(getFirestore(fireApp), "block", "vXLCRjlhOVW6oFOJvtmML6OolKA2")创建了一个DocumentReference ,因此查询从不包含该文档引用的posts中返回文档。

What you want to do instead is:你想要做的是:

  1. Load the blocked UIDs加载被阻止的 UID
  2. Pass them to the query将它们传递给查询

So in code:所以在代码中:

const myRef = doc(getFirestore(fireApp), "block", "vXLCRjlhOVW6oFOJvtmML6OolKA2");
const myDoc = await getDoc(myRef);
const blocked = myDoc.data().blockedUserId;
const q = query(
  collection(getFirestore(fireApp), "posts"),
  orderBy("uid"),
  where(
    "uid",
    "not-in",
    blocked
  )
)
// TODO: call getDocs or onSnapshot on q

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firestore 集合到具有 Firebase v9 的对象数组 - Firestore collection to array of objects with Firebase v9 如何将 Firestore 时间戳转换为 Date() - Firestore V9 - How to convert Firestore Timestamp to Date() - Firestore V9 Firestore——为 v9 重构一个 v8 getQuery - Firestore -- refactor a v8 getQuery for v9 Firestore v9 创建/更新子集合 - Firestore v9 create/update subcollection Firestore v9 预期 collection() 的第一个参数是 CollectionReference、DocumentReference 或 FirebaseFirestore - Firestore v9 Expected first argument to collection() to be a CollectionReference, a DocumentReference or FirebaseFirestore 在 Firebase Firestore 中查询整个集合时如何获取对象属性的子集? 使用JS客户端sdk v9 - How to get a subset of object's properties when querying an entire collection in Firebase Firestore? Using JS client sdk v9 如何在 javascript 上的 firebase v9 中获得授权用户 - How to get an authorized user in firebase v9 on javascript 如何在 firebase v9 中使用 useCollection react-firebase 钩子 - How to use useCollection react-firebase hook with firebase v9 如何从 firebase v9 获取服务器时间戳? - How to get server Timestamp from firebase v9? Not able to fetch data from Firebase Firestore SHOWS ERROR in NEXT.JS using Firebase SDK Firebase v9 - Not able to fetch data from Firebase Firestore SHOWS ERROR in NEXT.JS using Firebase SDK Firebase v9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM