简体   繁体   English

如何获取创建文档的用户的数据

[英]How to get the data of a user who created a document

I have two collections, one for registering properties and one for registered users.我有两个 collections,一个用于注册属性,一个用于注册用户。

The collection for registering properties is called listings and the collection for registered users is called users .注册属性的集合称为listings ,注册用户的集合称为users

I access the documents inside the listings collection using the parameter listingId , which is the document ID.我使用参数listingId访问listings集合中的文档,该参数是文档 ID。

I get the data from the document via the parameter I passed in:我通过传入的参数从文档中获取数据:

const fetchListing = async () => {
    const docRef = doc(db, 'listings', listingId)
    // response
    const docSnap = await getDoc(docRef)

    if (docSnap.exists) {
        listing = docSnap.data()
    }   
}

I need to get the fields (name,email, twitter, etc..) of the user who created the document.我需要获取创建文档的用户的字段(名称、email、twitter 等)。 How do I do this?我该怎么做呢?

Each document has the ID of the user who creates it.每个文档都有创建它的用户的 ID。

在此处输入图像描述

You have to do as follows:您必须执行以下操作:

  1. Use the Object returned by the data() method , to get the user ID value (As explained in the doc, "this method retrieves all fields in the document as an Object").使用data()方法返回的 Object 获取用户 ID 值(如文档中所述,“此方法将文档中的所有字段作为对象检索”)。
  2. Get the document corresponding to this user ID (exactly as you do to get the listing doc) and then, get the data of this document (again with the data() method).获取与此用户 ID 对应的文档(与获取列表文档完全相同),然后获取此文档的数据(再次使用data()方法)。

  const fetchListing = async () => {
    const docRef = doc(db, 'listings', listingId);
    const docSnap = await getDoc(docRef);

    if (docSnap.exists) {
      const userId = docSnap.data().user;

      const userDocRef = doc(db, 'users', userId);
      const userDocSnap = await userDocRef(docRef);

      if (userDocSnap.exists) {
        const userName = docSnap.data().name;
        const userEmail = docSnap.data().email;
        // ...
      }
    }
  };

Note that a common approach for your use case in the NoSQL world is to denormalize your data in such a way that you get all the necessary data in your front-end in a minimum number of queries.请注意,在 NoSQL 世界中,您的用例的一种常见方法是对您的数据进行非规范化处理,以便您以最少的查询次数在前端获取所有必要的数据。 Here is a "famous" post about NoSQL data-modeling approaches.这是一篇关于 NoSQL 数据建模方法的“著名” 帖子

More concretely it means that in each Listing doc you would duplicate the data of its author/creator.更具体地说,这意味着在每个列表文档中,您将复制其作者/创建者的数据。

One side effect of this approach is that you need to keep the values in sync (ie the one in the User document and the ones in the Listing documents).这种方法的一个副作用是您需要保持值同步(即用户文档中的值和列表文档中的值)。 This synchronization could be done with a Cloud Function triggered if the User doc is modified.如果修改了用户文档,则可以通过触发 Cloud Function 来完成此同步。

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

相关问题 如何获得创建帖子的用户的帖子列表? - How can I get a list of posts with the user who created them? 获取与创建帖子的用户匹配的用户的数据 - Getting data of a user that matches the user who created a post 如何从手动创建的文档中获取样式表? - How to get the stylesheets from a manually created document? 如何获取在 Discord.js 中发送消息的用户的角色? - How to get the roles of user who messaged in Discord.js? 如何获取登录当前会话的用户的ID? - How to get the ID of the user who is logged into the current session? 如何从 Calendly webhook 获取举办活动的用户? - How to get User who hosts the event from a Calendly webhook? 如何为必须能够编辑文档的用户禁用从Google表格复制功能? - How to disable copying from google sheets for a user who must be able to edit the document? Mongoose:如何将之后创建的文档中的数据传递到之前创建的文档中? - Mongoose: How do I pass data from a document created afterwards into a document created right before? 如何为刚刚创建的用户获取uId - How to get the uId for the user just created 如何获取用户点击的文档的文档ID? - How to get the document id of the document on which user clicks?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM