相关疑难解决方法(0)

firestore云函数onCreate/onDelete有时会立即触发两次

我偶尔会在onCreate和onDelete触发器上观察到这种行为. 在此输入图像描述

两个执行都发生在firestore中创建的同一文档中.那里只有一个文档,所以我不明白它是如何触发处理程序两次的.处理程序本身很简单:

module.exports = functions.firestore.document('notes/{noteId}').onCreate((event) => {
  const db = admin.firestore();
  const params = event.params;
  const data = event.data.data();
  // empty
});
Run Code Online (Sandbox Code Playgroud)

这不会一直发生.我错过了什么?

firebase google-cloud-functions google-cloud-firestore

9
推荐指数
1
解决办法
3170
查看次数

具有Idempotency的云功能和Firebase Firestore

我正在使用带有云功能的测试版Firestore.在我的应用程序中,我需要触发一个侦听onCreate事件/company/{id}/point/{id}并执行插入的函数(collection('event').add({...}))

我的问题是:使用Firestore的云功能需要幂等功能.我不知道如何确保如果我的函数连续两次使用相同的事件触发,我将不会添加两个具有相同数据的文档.

我发现context.eventId可以解决这个问题,但我不认识一种使用它的方法.

exports.creatingEvents = functions.firestore
  .document('/companies/{companyId}/points/{pointId}')
  .onCreate((snap, context) => {

    //some logic...

    return db.doc(`eventlog/${context.params.companyId}`).collection('events').add(data)
})
Run Code Online (Sandbox Code Playgroud)

idempotent firebase google-cloud-functions google-cloud-firestore

6
推荐指数
1
解决办法
610
查看次数

Firebase云功能多次调用

当我没有检查previous.exists()时,我的firebase云函数被多次调用.我收到多个推送通知.

if (!event.data.exists()){
    return;
}
if (event.data.previous.exists()){
    return;
}
Run Code Online (Sandbox Code Playgroud)

但是当我检查它时,我没有得到推送通知.这是不工作的代码:我应该改变什么?

exports.sendShoppingListInvitationNotification = functions.database.ref('/invites/{id}/').onWrite(event => {
//get the snapshot of the written data
const snapshot = event.data;  

if (!event.data.exists()){
    return;
}
if (event.data.previous.exists()){
    return;
}

    //get snapshot values
    console.log(snapshot.key);
const receiptToken = snapshot.child('receiptFcmToken').val();
const senderName = snapshot.child('senderNickname').val();
const inviteMessage = snapshot.child('inviteMessage').val();
const senderImage = snapshot.child('senderProfileImageURL').val();


//create Notification
const payload = {
    notification: {
        title: `Invitation from ${senderName}`,
        body:  `${inviteMessage}`,
        icon: `${senderImage}`,
        badge: '1',
        sound: 'default',
    }
};               

//send a notification to …
Run Code Online (Sandbox Code Playgroud)

firebase google-cloud-functions firebase-cloud-messaging

4
推荐指数
1
解决办法
2521
查看次数

如何正确处理 Firebase Cloud Functions 的幂等性?

我已经看到了所有这些问题和资源,但都不能满足我的担忧。

具有幂等性的云函数和 Firebase Firestore

文档中的 Firebase 云函数幂等性

如何在 Cloud Functions 中进行幂等聚合?

https://cloud.google.com/functions/docs/bestpractices/retries#make_retryable_background_functions_idempotent

如果一个函数被多次执行并且同时运行,我怎么知道另一个正在运行并安全地丢弃它?即使我eventId向 DB写入某处,在我设法写入此类信息之前,其他函数仍有可能运行。

使用 Firestore 事务可以帮助解决这个问题吗?如果我要基于eventId事务内部写入文档,那么在我释放“锁定”之前是否可以说任何其他功能将保持安全?

使用的方法.set对我来说是不可行的,因为某些函数正在为文档生成一个唯一的 ID,所以无论如何我最终都会多次编写这些 ID。其他一些功能非常复杂,可以同时生成(或者说转换)一堆文档。

尽管我喜欢云函数的整个想法,但我希望有更多针对此的成熟解决方案。为什么 Firebase 基本上不能做他们想从开发者那里得到的东西?他们可以写入eventId一些内部数据库并防止多次运行函数。强迫每个人自己处理它听起来真的很愚蠢。

firebase google-cloud-functions

1
推荐指数
1
解决办法
294
查看次数