简体   繁体   English

读取文档 firestore 的子集合中的文档(V9(模块化))

[英]Read documents in the sub-collections of a document firestore (V9 (Modular))

I have a collection of users.我有一组用户。 Each document in that collection contains user data.该集合中的每个文档都包含用户数据。 Inside of that document, there is information about their username, bio, etc. Also, there is a sub collection inside the user document called posts.在该文档中,有关于他们的用户名、简历等的信息。此外,在用户文档中还有一个名为 posts 的子集合。 And each document inside posts is an individual post the user made. posts 中的每个文档都是用户发布的一个单独的帖子。

Currently, my program is reading live updates to check for changes in the username and bio.目前,我的程序正在读取实时更新以检查用户名和简历的变化。 However, I want it to also check for live updates in within the posts collection to see if there has been a new post.但是,我希望它还检查帖子集合中的实时更新,看看是否有新帖子。 So that it knows to display a new post in the users profile.这样它就知道在用户配置文件中显示新帖子。

Here is my current code for checking for updates:这是我当前用于检查更新的代码:

const [name, setName] = useState('');
const [bio, setBio] = useState('');

// Post title
const [p_title, setP_title] = useState('');

// Hears for when there's been an update
useEffect(() => {
    const unsubscribe = onSnapshot(doc(db, "usrs", usrUID), (doc) => {
        setName(doc.data().name);
        setBio(doc.data().bio);

        console.log(doc.data());
    });

    // Unsubscribe stops it from listening when it shouldn't
    return unsubscribe
}, []);

I was hoping there was some type of thing like:我希望有某种类型的东西:

doc.data().collection("Posts").posts.title

or something like that so that I could list the title of the posts from the subcollection, as well as the name and bio.或类似的东西,以便我可以列出子集合中帖子的标题,以及名称和简介。

I am quite new to React native and firestore.我对 React native 和 firestore 很陌生。 I often feel like I'm using a different version to a lot of the information that is out there.我常常觉得我正在使用不同版本的大量信息。

Any help would be appreciated!任何帮助,将不胜感激!

One onSnapshot() can listen updates for the given document or collection only.一个onSnapshot()只能侦听给定文档或集合的更新。 So for a user's posts you'll need another listener.因此,对于用户的帖子,您需要另一个侦听器。 Try:尝试:

// Listen for user document updates
const unsubUserDoc = onSnapshot(doc(db, "usrs", usrUID), (doc) => {
  setName(doc.data().name);
  setBio(doc.data().bio);

  console.log(doc.data());
});

// Listen for user's post changes
const unsubPostsCol = onSnapshot(collection(db, "usrs", usrUID, "posts"), (querySnap) => {
  console.log(querySnap.docs.map((d) => ({
    id: d.id,
    ...d.data()
  })));

  // set all docs in state
});

The second listener will trigger every time a user's post is created/updated/deleted.每次创建/更新/删除用户的帖子时都会触发第二个侦听器。 This will also fetch all the posts once initialised so it might be best to paginate the results wherever possible.这也将在初始化后获取所有帖子,因此最好尽可能对结果进行分页。

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

相关问题 从 Firestore 中的多个子集合的多个子集合中获取所有文档的最有效方法是什么? - What is the most efficient way to get all documents from multiple sub-collections of multiple sub-collections in Firestore? 使用子 collections >> 子文档从 firebase 文档读取数据 - Read data from firebase document with sub collections >> sub documents Firestore 使用 Firebase Client SDK V9 Modular with Admin 权限 - Firestore using Firebase Client SDK V9 Modular with Admin Privileges 使用 Firebase v9 访问包含 collections 的嵌套文档数组 - Accessing nested array of documents containing collections with Firebase v9 无法读取 firebase v9 {modular} 中未定义的属性(读取“indexOf”) - Cannot read properties of undefined (reading 'indexOf') in firebase v9 {modular} 从 Firebase Firestore 中删除字段/键带有句点/标点符号 (".") 的字段 - 模块化 v9 JavaScript SDK - Delete a field from Firebase Firestore where the field/key has a period/punctuation (".") - modular v9 JavaScript SDK 在子集合更新时触发云 function - Trigger cloud function on sub-collections update firebase V9更新文档 - Update documents in firebase V9 Firestore——为 v9 重构一个 v8 getQuery - Firestore -- refactor a v8 getQuery for v9 Firestore v9 创建/更新子集合 - Firestore v9 create/update subcollection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM