【问题标题】:How to add a collection to another in cloud firestore?如何将一个集合添加到 Cloud Firestore 中的另一个集合?
【发布时间】:2020-08-10 02:16:03
【问题描述】:

在 cloud firestore 中,我想将值形式添加到集合 A,然后将所有相同的值添加到另一个集合 B,但集合 B 在集合 A 内。 目前我想在将数据添加到集合 A 时获取文档的 ID,然后使用 this.firestore.collection(A).doc(id).collection(B).add(data),但目前我无法获取文件标识 这是我在将数据添加到集合 A 时获取 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);
    }

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    你可以做第二个添加里面第一个的回调。另请注意,您应该在回调中使用箭头函数来从外部范围保留 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);
                });
    

    【讨论】:

    • 如何指定字段类型?说数据应该只作为字符串添加
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 2019-10-01
    • 2022-01-19
    • 2020-05-25
    • 1970-01-01
    • 2022-01-11
    • 2013-07-31
    相关资源
    最近更新 更多