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 数据库触发器 - 如何等待来电

最新更新