Firebase数据库触发器-在删除和更新时触发onCreate

柯密特

我有三个Firebase数据库触发器功能,可以将通知推送给用户。但是,我注意到.onCreate()在数据库更新和删除时被触发。这是预期的吗?有办法防止这种情况吗?

const functions = require('firebase-functions')

exports.onNoteCreate = functions
.region('europe-west1')
.database
.ref('/notes/{noteId}')
.onCreate((snapshot, context) => {
  ...
  //Push notification to affected users

  //Compose notification object
  const notificationObject = { "test": true }
  membersToAlert.forEach((memberId, index) => {
    let isAlreadyPresent = false
    //Do not write if already present! - This code should not be needed?
    const ref = snapshot.ref.root.child(`/notes/${personId}/noteAdditions`)
    ref.orderByChild('originId')
      .equalTo(noteId)
      .on("value", (removeSnapshot) => {
        isAlreadyPresent = true
      })
    //Write notification to all affected users
    if(!isAlreadyPresent) {
      snapshot.ref.root.child(`/notifications/${personId}/noteAdditions`).push(notificationObject)
    }
  })
  return true
})

我的.onUpdate()和.onDelete()触发器也正在监听.ref('/ notes / {noteId}')。那是问题吗?

如何确保.onCreate()插入对象时被触发

编辑:

我的测试工作流程如下:

  1. 使用.push()在/ notes中创建一个新节点->按预期工作

  2. 使用.update()更新同一节点->按预期工作

  3. 直接从Firebase控制台删除/ notes / {noteId}中的节点

步骤3触发器.onCreate()和.onUpdate()。请参阅以下日志:

I 2019-08-12T17:17:25.867Z onNoteCreate 670055884755913 onNoteCreate ... onNoteCreate 670055884755913 
I 2019-08-12T17:17:26.053Z onNoteUpdate 670048941917608 onNoteUpdate ... onNoteUpdate 670048941917608 
D 2019-08-12T17:17:26.843878505Z onNoteDelete 670054292162841 Function execution started onNoteDelete 670054292162841 
D 2019-08-12T17:17:26.849773576Z onNoteDelete 670054292162841 Function execution took 7 ms, finished with status: 'ok' onNoteDelete 670054292162841

删除前的数据库

-notifications
  -userId
    -noteAdditions
      -guid01
        -notificationData
    -noteUpdates
      -guid03
        -notificationData

删除后的数据库

//guid01 gets deleted by .onDelete() as expected
//guid03 gets deleted by .onDelete() as expected

-notifications
  -userId
    -noteAdditions
      -guid02
        -notificationData //Inserted by .onCreate() upon delete
    -noteUpdates
      -guid04
        -notificationData //Inserted by .onUpdate() upon delete

侦听器附加到/ notes / {noteId},并且正在/ notifications / {userId} /进行更新。

onNoteCreate

exports.onNoteCreate = functions
.region('europe-west1')
.database
.ref('/notes/{noteId}')
.onCreate((snapshot, context) => {
  ...
  snapshot.ref.root.child(`/notifications/${personId}/noteAdditions`).push(notificationObject)
  ...
  console.log('onNoteCreate', '...')
  ...
})

onNoteUpdate

exports.onNoteUpdate = functions
.region('europe-west1')
.database
.ref('/notes/{noteId}')
.onUpdate((change, context) => {
  ...
  change.after.ref.root.child(`/notifications/${personId}/noteUpdates`).push(notificationObject)
  ...
  console.log('onNoteUpdate', '...')
  ...
})

这样导入函数是否重要?

const create = require('./src/db-functions/notes').onNoteCreate
const update = require('./src/db-functions/notes').onNoteUpdate
const delete = require('./src/db-functions/notes').onNoteDelete
exports.onNoteCreate = create
exports.onNoteUpdate = update
exports.onNoteDelete = delete
柯密特

我在我调用的示例中未包含代码

  //Get user data - also updated by onNoteCreate, onNoteUpdate , onNoteDelete
  dbRoot.child(`users/${userId}`)
  .on('value', (snapshot) => {

.on()附加了一个在每次更新值时都会触发的侦听器,因此在不期望时会触发“ onNoteCreate”,“ onNoteUpdate”和“ onNoteDelete”。如果我不希望附加一个我不想要的监听器,则应该使用.once()

在此帖子中向我指出的所有@功劳@doug:Firebase数据库触发器-如何等待呼叫

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Firebase数据库触发器:onCreate,onUpdate,onDelete

使用Firebase数据库触发器onUpdate

用于Firebase数据库的Cloud Functions触发器?

OnUpdate Firebase数据库触发器

数据库多表触发器

SQL触发器数据库

Firebase 实时数据库 - 数据库触发器结构最佳实践

Firebase 数据库触发器 - 从快照中获取数据库对象

比较Firebase数据库的云函数触发器onCreate(),onWrite(),onUpdate(),何时使用?

如何在onCreate触发器上访问Firebase云功能数据库的值

Firebase Cloud Functions数据库触发器'onCreate不是函数'

Swift Firebase 更新旧数据 - 实时数据库触发器

不使用数据库触发器就无法使用Firebase云功能更新Firebase数据库

如何使用Firebase Cloud函数http触发器从Firebase数据库获取数据

实时数据库触发器和推送通知Firebase和iOS

Firebase云功能数据库触发器有时无法正确运行

在 Firebase 函数中为数据库触发器传递的数据是否计入传出带宽?

Firebase的Cloud Functions-从数据库触发器获取父数据

使用触发器向Firebase实时数据库添加元素

从Cloud Functions for Firebase中的数据库触发器中获取用户ID?

如何使用Admin SDK为数据库触发器设置Firebase FCM

Firebase数据库触发器-如何等待调用

Cloud Run 或 Kubernetes 上的 Firebase 实时数据库触发器,而不是 Cloud Functions

SQL Server:触发器与数据库触发器

插入触发器时数据库phpmyadmin

在PHP的何处放置数据库事件触发器?

在GoSQL中处理数据库触发器

触发器阻止数据库项目发布

获取数据库表的触发器名称