简体   繁体   English

在谷歌云 Firestore 中获取 promise 中增加的计数器值

[英]Obtain incremented counter value in promise in Google cloud Firestore

I am trying to create a new invoice number by using the incremented counter value.我正在尝试使用递增的计数器值创建一个新的发票编号。 How can I obtain the incremented value (in a promise) after incrementing it?递增后如何获得增量值(在承诺中)?

Below is the code that I am using to increment.下面是我用来递增的代码。

db.doc(docRef).update({numberOfInvoices: FieldValue.increment(1)});

Firestore counters Firestore 柜台

You need to query the document after you have updated it, as follows:更新后需要查询文档,如下:

db.doc(docRef).update({numberOfInvoices: FieldValue.increment(1)})
.then(() => {
   return db.doc(docRef).get();
})
.then(doc => {
        console.log("Document data:", doc.data());
}).catch(error => {
    // ...
});

FieldValue.increment "returns a special value that can be used with set() or update() that tells the server to increment the field's current value by the given value". FieldValue.increment “返回一个可以与set()update()一起使用的特殊值,它告诉服务器将字段的当前值增加给定值”。 In other words, the calculation is done by the Firestore backend, and therefore you need to query the doc to get the new incremented value.换句话说,计算是由Firestore后端完成的,因此您需要查询文档以获取新的增量值。

You can just get the incremented value by您可以通过以下方式获得增量值

let data =await db.doc(docRef).get(); //make sure the functions is async
let counts = data.val()['numberOfInvoices'];

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

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