我正在使用Angular和Firebase开发发票应用程序。我需要计算集合中当前有多少行+ 1才能获得我的发票ID。问题是,如果我们有新的一年,该计数器应从1重新开始。我以为我可以做类似invoices / 2019这样的事情,它只是一个集合中的一个集合,因此我可以轻松地计算第二个集合中的所有行,但不能在一个集合中进行一个集合。因此,我制作了以下发票/ 2019 / october / id,这是一个托收/文档/托收/文档。 2019年将是当前年份,十月将是当前月份。现在我不知道如何计算本年度的每张发票。而且我不确定这是否是最好的解决方案。
// How I create an entry to the database
currentYear: string = this.datepipe.transform(new Date, 'yyyy');
currentMonth: string = this.datepipe.transform(new Date, 'MMMM');
this.afs.collection('invoices').doc(this.currentYear).collection(this.currentMonth).add(data);
// Currently I use the following which outputs 2019-001 and so on
this.afs
.collection('invoices')
.valueChanges()
.subscribe(res => {
this.invoiceID = res.length + 1;
const invoiceIDFormatted = ('00' + this.invoiceID).slice(-3);
this.service.form
.get('invoiceID')
.setValue(currentDateYear + '-' + invoiceIDFormatted);
});
这当前适用于发票/ id,但我需要使其与发票/ 2019 / october / id一起使用,这意味着它必须经过所有月份。