简体   繁体   English

Firebase - 无法嵌套集合集合

[英]Firebase - unable to nest a collection of collection

In my React Native project I am using Firestore as my database.在我的 React Native 项目中,我使用 Firestore 作为我的数据库。 Whenever a user registers I want to create a supervisor approval request.每当用户注册时,我都想创建一个主管批准请求。

Basically under a collection of supervisorRequests there should be a document for each supervisor , named after the supervisor's key.基本上,在supervisorRequests的集合下,应该有每个supervisor的文档,以主管的密钥命名。 Under the supervisor document I want to have a collection of randomly generated ids which each hold the request metadata.在主管文档下,我希望有一组随机生成的 ID,每个 ID 都保存请求元数据。 Here is my desired structure:这是我想要的结构:

-supervisorRequests
      -supervisor-1-Key
           -random-id-of-request
              - user: userID
              - userEmail: userEmail
              - requestDate: timestamp
           -random-id-of-request
              - user: userID
              - userEmail: userEmail
              - requestDate: timestamp
      -supervisor-2-Key
           -random-id-of-request
              - user: userID
              - userEmail: userEmail
              - requestDate: timestamp
           -random-id-of-request
              - user: userID
              - userEmail: userEmail
              - requestDate: timestamp
           -random-id-of-request
              - user: userID
              - userEmail: userEmail
              - requestDate: timestamp

My code trying to achieve this is:我试图实现这一目标的代码是:

const docReference = firebase.firestore().collection(`supervisorRequests`).doc(this.props.supervisorKey);
docReference.set({ user: this.props.userUID, requestDate: new Date().getTime(), userEmail: this.props.email });

My code, however, generates the following structure:但是,我的代码生成以下结构:

-supervisorRequests
      -supervisor-1-Key
          - user: userID
          - userEmail: userEmail
          - requestDate: timestamp

This is not what I want since every time a new request is made to the supervisor's key, the old request is overriden.这不是我想要的,因为每次向主管的密钥发出新请求时,旧请求都会被覆盖。

What would be wrong with my code and how can I achieve the first database structure I have presented?我的代码有什么问题,我怎样才能实现我提出的第一个数据库结构?

The string you pass to collection() must be a path to a collection.您传递给 collection() 的字符串必须是集合的路径。 What you're passing now is the path to a document.您现在传递的是文档的路径。 It discerns this by seeing the forward slash in the string.它通过查看字符串中的正斜杠来识别这一点。 Perhaps you want to reference the document like this instead:也许您想像这样引用文档:

firebase.firestore()
        .collection('supervisorRequests')
        .doc(this.props.supervisorKey)

You can use the returned DocumentReference to create the document with its set() method.您可以使用返回的 DocumentReference 通过其 set() 方法创建文档。

Just to complete Doug's answer, note that you can directly pass a "slash-separated path" to the doc() method, in order to get "a DocumentReference instance that refers to the document at the specified path".只是为了完成 Doug 的回答,请注意,您可以直接将“斜线分隔的路径”传递给doc()方法,以获取“引用指定路径处的文档的DocumentReference实例”。

So you could also do:所以你也可以这样做:

firebase.firestore().doc(`supervisorRequests/${this.props.supervisorKey}`).set();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM