【问题标题】:Angular Firestore - Add new document with subcollection at same timeAngular Firestore - 同时添加带有子集合的新文档
【发布时间】:2018-04-12 09:18:55
【问题描述】:

我正在尝试在“发票”集合中创建一个新文档,该文档本身可以正常工作,同时我还尝试在此新文档中添加一个“项目”子集合,它本身也可以正常工作。

但是,当我尝试同时执行这两项操作时,只有项目集合出现在 firestore 控制台的新文档中,考虑到我可以在“查看发票”中访问文档的详细信息,这更加奇怪应用程序的组件,它不显示项目。

我试图通过获取将要创建的新文档的引用来做到这一点,然后参考生成的新文档 ID/引用来设置这个新的子集合,这似乎可行,但又一次,同时尝试两者不会用主数据子集合填充文档。

我还尝试将添加子集合的方法放在初始文档创建的 then() 方法中,以解决导致此问题的潜在异步问题,但这产生了相同的结果,只是添加了子集合.

组件:

save() {

    let _this = this;

    let invoiceSubtotal = 0;
    let invoiceTax = 0;
    let invoiceTotal = 0;

    for (let item of this.items) {
        invoiceSubtotal += item.subtotal;
        invoiceTax += item.tax;
        invoiceTotal += item.total;
    }

    this.invoice = {
        id: null,
        invoiceId: 999,
        reference: this.newInvoiceForm.get('reference').value,
        date: new Date(this.newInvoiceForm.get('date').value),
        contact: this.selectedContact,
        description: this.newInvoiceForm.get('reference').value,
        subtotal: +invoiceSubtotal,
        tax: +invoiceTax,
        total: +invoiceTotal,
        paid: false
    }

    this.db.collection('/users').doc(this.userId).collection('/invoices').add(this.invoice)
        .then(function(docRef) {
            _this.notifService.showNotification('New invoice created', null);
        })
        .catch(function(error) {
            _this.notifService.showNotification('Error creating invoice: ' + error.message, null);
        })

    let ref = _this.db.collection('/users').doc(_this.userId).collection('/invoices').ref.doc().id;
    console.log('ref: ' + ref);

    for (let item of _this.items) {
        _this.db.collection('/users').doc(_this.userId).collection('/invoices').doc(ref).collection('/items').add(item)
            .then(function(docRef) {
                console.log('document added to items collection: ', item);
            })
            .catch(function(error) {
                console.error('Error adding document to items collection: ' + error.message);
            })

    }

}

【问题讨论】:

    标签: angular typescript firebase asynchronous google-cloud-firestore


    【解决方案1】:

    我已经通过使用不同的方法来设置文档数据解决了这个问题。

    我没有使用collection().add() 方法直接添加文档,而是使用了我必须将子集合设置为doc(ref).set(this.invoice) 的参数的“ref”变量,因此这绝对引用了同一个文档引用项目子集合的项目被推到了下方。

    let ref = _this.db.collection('/users').doc(_this.userId).collection('/invoices').ref.doc().id;
        console.log('ref: ' + ref);
    
        this.db.collection('/users').doc(this.userId).collection('/invoices').doc(ref).set(this.invoice)
            .then(function(docRef) {
                _this.notifService.showNotification('New invoice created', null);
            })
            .catch(function(error) {
                _this.notifService.showNotification('Error creating invoice: ' + error.message, null);
            })
    
        for (let item of _this.items) {
            _this.db.collection('/users').doc(_this.userId).collection('/invoices').doc(ref).collection('/items').add(item)
                .then(function(docRef) {
                    console.log('document added to items collection: ', item);
                })
                .catch(function(error) {
                    console.error('Error adding document to items collection: ' + error.message);
                })
    
        }
    

    【讨论】:

      【解决方案2】:

      这是一个老问题,但这是供观众参考的。 使用firestore Batch 将是一个更好的主意。创建一个批次并添加主文档,然后添加子集合文档。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-08-25
        • 1970-01-01
        • 2021-05-30
        • 1970-01-01
        • 1970-01-01
        • 2018-05-11
        相关资源
        最近更新 更多