简体   繁体   English

如何将一个集合添加到云 Firestore 中的另一个集合?

[英]How to add a collection to another in cloud firestore?

In cloud firestore, I want to add the value form to collection A and then all the same value to another collection B, but collection B is inside collection A. Currently I want to get the id of the document while adding the data to collection A and then use this.firestore.collection(A).doc(id).collection(B).add(data), but currently I cannot get the ID of the document This one is my code to get ID when adding data to collection A.在cloud firestore中,我想将值形式添加到集合A,然后将所有相同的值添加到另一个集合B,但集合B在集合A中。目前我想在将数据添加到集合A时获取文档的ID然后使用this.firestore.collection(A).doc(id).collection(B).add(data),但目前我无法获取文档的ID 这是我在将数据添加到集合时获取ID的代码一个。

onSubmit(form: NgForm) {
        let data = form.value;
        lastID = '';
        this.firestore
            .collection('Users')
            .add(data)
            .then(function (docRef) {
                this.lastID = docRef.id;
            });
        this.firestore.collection('Users').doc(lastID).collection('profile').add(data);
        this.resetForm(form);
    }

You can do the second add inside the callback of the first.您可以第一个回调中进行第二个添加。 Also note that you should use an arrow function in your callback to preserve the value of this from the outer scope:另请注意,您应该在回调中使用箭头 function 以保留外部this的值:

        this.firestore
            .collection('Users')
            .add(data)
            .then(docRef => {
                this.lastID = docRef.id;
                return this.firestore.collection('Users').doc(this.lastID).collection('profile').add(data);
            })
            .then(() => {
                this.resetForm(form);
            });

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

相关问题 Cloud Firestore:一次性添加集合和子集合 - Cloud Firestore: Add collection and sub collection in one go Firestore如何添加到子集合 - Firestore how to add to a sub collection 如何通过云触发 Firestore 收集 function - how to trigger a firestore collection via cloud function 如何将另一个集合中的数据添加到检索到的 Firestore onSnapshot 集合(如外键关系)? - How to add data from another collection to a retrieved Firestore onSnapshot collection (like foreign key relationship)? 用户在 Cloud firestore 中注册时如何将当前用户 uid 添加到用户集合? - how to add current user uid to user collection when user signup in cloud firestore? 使用Google Cloud Functions从另一个Firestore集合中引用的字段在Firestore集合中创建字段 - Creating a field in a Firestore collection from a field referenced in another Firestore collection using Google Cloud Functions 如何在没有任何身份验证的情况下保护 Cloud Firestore 集合和子集合? - How to secure Cloud Firestore collection and sub collection without any auth? 如何使用firebase firestore有效地将项目添加到集合中 - How to efficiently add items to collection with firebase firestore 如何连续将对象添加到 Firestore 中的嵌套集合 - How to continuously add objects to nested collection in Firestore 获取 Cloud Firestore 数据库集合 - Get Cloud Firestore database collection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM