Firebase数据库触发器-在删除和更新时触发onCreate [英] Firebase database trigger - onCreate gets triggered on delete and update

查看:62
本文介绍了Firebase数据库触发器-在删除和更新时触发onCreate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have three Firebase database trigger function to push notifications to users. However, I have noticed that .onCreate() gets triggered on database update and delete. Is this expected? Is there a way to prevent this?

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}').那是问题吗?

My .onUpdate() and .onDelete() triggers are also listening to .ref('/notes/{noteId}'). Is that a problem?

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

How can I make sure .onCreate() only gets triggered when a new object is inserted?

我的测试工作流程如下:

My testing workflow is as follows:

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

  1. Create a new node in /notes using .push() -> works as expected

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

Update the same node using .update() -> works as expected

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

Delete the node in /notes/{noteId} directly from the Firebase Console

第3步同时触发两者 .onCreate()和.onUpdate().请参阅下面的日志:

Step 3 triggers both .onCreate() and .onUpdate(). See log below:

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}/...上进行更新.

The listeners are attached to /notes/{noteId} and updates are being made at /notifications/{userId}/...

onNoteCreate

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

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', '...')
  ...
})

这样导入函数是否重要?

Does it matter that I import the functions like so?

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

推荐答案

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

I failed to include the code in my example where I call

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

附加了

="a href =" https://firebase.google.com/docs/reference/admin/node/admin.database.Query#on"rel =" nofollow noreferrer>.on()每次更新值时都会触发的侦听器,因此在不期望时会触发"onNoteCreate","onNoteUpdate"和"onNoteDelete".我应该使用 .once()如果我不想附加一个我不想要的听众.

.on() attached a listener that was being triggered each time the value was updated, thus triggering "onNoteCreate", "onNoteUpdate" and "onNoteDelete" when not expected. I should have used .once() if I did not wish to attach a listener which I did not.

在此帖子中向我指出的所有@doug积分: Firebase数据库触发器-如何等待调用

All credits to @doug for pointing this out to me in this post: Firebase database trigger - how to wait for calls

这篇关于Firebase数据库触发器-在删除和更新时触发onCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆