Firebase 如何使用Google云功能在Firestore上创建的文档中添加新值

Firebase 如何使用Google云功能在Firestore上创建的文档中添加新值,firebase,google-cloud-firestore,google-cloud-functions,Firebase,Google Cloud Firestore,Google Cloud Functions,我想添加一个累进计数器,如“G0001”、“G0002”…'G0010'在我的Firestore上创建文档时。我使用Firestore API在我的web应用程序上创建此文档。我希望在创建新文档并设置新字段(如{'code':'G0001'})时触发该函数。此外,我必须阅读Firestore中的文档才能添加最后一个代码 示例: 通常在web应用程序上创建的文档: { 'name': 'Augusto', 'age': 28, } 要存储在Firestore上的文档已按功能更改 { 'name'

我想添加一个累进计数器,如“G0001”、“G0002”…'G0010'在我的Firestore上创建文档时。我使用Firestore API在我的web应用程序上创建此文档。我希望在创建新文档并设置新字段(如
{'code':'G0001'}
)时触发该函数。此外,我必须阅读Firestore中的文档才能添加最后一个代码

示例:

通常在web应用程序上创建的文档:

{
'name': 'Augusto',
'age': 28,
}
要存储在Firestore上的文档已按功能更改

{
'name': 'Augusto',
'age': 28,
'code': 'G0001'
}
我用
firebase init
和我的
索引创建了一个节点项目。ts
拥有它:

import * as functions from 'firebase-functions';

exports.createNewGrupoDeInsumos = functions.firestore
.document('collection-name')
.onCreate((snap,context) => {

});

ps:我不知道我什么时候设置集合名称,我想这是如上所述。

您需要在数据库中维护一个全局计数器。为此,只需创建一个文档,例如在名为
counterDocCollection
的集合下,id等于
counterDocCollection
和名为
counter
的数字字段

然后,在云函数中,您需要增加计数器,读取新值并使用它生成
Gxxxx

根据此计数器的更新频率,有两种方法。事实上,在Firestore中,您只能每秒更新一次单个文档。因此,这两种方法是:

  • 如果需要每秒更新(单个)计数器文档一次以上,则需要使用分布式计数器。有一个专用的文档项,请参阅
  • 如果更新计数器文档的频率较低,则可以使用。另见

  • 要获取计数器文档,只需使用Admin SDK,如下所示:

    const counterDoc = admin.firestore().collection('counterDocCollection').doc('counterDocCollection');
    
    import * as functions from 'firebase-functions';
    //.....
    
    const FieldValue = require('firebase-admin').firestore.FieldValue;
    //.....
    
    exports.createNewGrupoDeInsumos = functions.firestore
    .document('collection-name')
    .onCreate((snap,context) => {
    
        //.....
    
        const counterDoc = admin.firestore().collection('counterDocCollection').doc('counterDoc');
    
        return counterDoc.update({
           counter: FieldValue.increment(1);
        })
        .then(() => {
            return counterDoc.get();
        })
        .then(counterDoc => {
           const newCounterValue = doc.data().counter;
    
           //use newCounterValue to generate the Gxxxx value
           //Set the value of code in your initial doc
    
           return .....
    
    });
    

    要在云函数中使用该方法,请执行以下操作:

    const counterDoc = admin.firestore().collection('counterDocCollection').doc('counterDocCollection');
    
    import * as functions from 'firebase-functions';
    //.....
    
    const FieldValue = require('firebase-admin').firestore.FieldValue;
    //.....
    
    exports.createNewGrupoDeInsumos = functions.firestore
    .document('collection-name')
    .onCreate((snap,context) => {
    
        //.....
    
        const counterDoc = admin.firestore().collection('counterDocCollection').doc('counterDoc');
    
        return counterDoc.update({
           counter: FieldValue.increment(1);
        })
        .then(() => {
            return counterDoc.get();
        })
        .then(counterDoc => {
           const newCounterValue = doc.data().counter;
    
           //use newCounterValue to generate the Gxxxx value
           //Set the value of code in your initial doc
    
           return .....
    
    });
    

    也许您可以通过查询获取集合并按“代码”对其进行排序,以获得最大值您所说的“我不知道什么时候设置集合名称,我想这是如上所述”的确切含义是什么?我已经写了一个答案,请参见下文。我建议您尝试实现这些方法中的一种,如果遇到一些问题,请寻求帮助(并共享您现有的代码:-),我有一些问题。1.这个
    .document('collection-name')
    是错误的,因为我发现G.C.F.不能引用集合,只能引用
    .document('collection-name/docName')
    之类的文档,但我可以解决它(我想)。2.这个函数必须返回什么?为什么有两个
    then()
    ?3.我如何将值设置为文档计数器,并将新值
    GXXXX
    设置为我创建的特定文档?4.功能是最好的方式吗?我也可以在本地应用上做。这是很多问题:-)让我们逐一回答。1/是的,Cloud Firestore触发器必须始终指向文档,而不能指向集合。但是您可以使用通配符。请参阅2/Firestore方法(即
    update()
    get()
    等)是异步的,返回
    promises()
    。在云功能中,您需要链接这些承诺。查看官方视频系列了解更多信息:3/我建议您试一试!:-)您将通过snap获取当前文档,snap是一个
    DocumentSnapshot
    。请参阅和
    ref
    属性。然后使用
    update()
    方法。如果您在尝试时遇到问题,我很乐意查看您的代码!4/这取决于此计数器是否由所有用户共享。如果在本地应用程序上增加计数器,两个用户可能会生成相同的计数器值。