My data structure is the following: compliments/received/{uid}/{complimentId}
我想听听有关恭维/已收到/{uid}/{complimentId}/updatedAt 的更改
我的代码是:
firebase.database().ref('compliments/received/' + currentUser._id).on('child_changed', (snap) => {
console.log('update in action', snap.val())
snap.ref.child('updatedAt').on('value', () => onUpdate(snap))
});
问题:
当我更新给定数据库路径上的任何字段时,例如:当我将“seen”从 0 设置为 1 时,此侦听器会触发。我想仅在 UpdatedAt 字段的值更改时触发此侦听器。
到目前为止我尝试过的:
firebase.database().ref('compliments/received/' + currentUser._id).on('child_changed', (snap) => {
console.log('update in action', snap.val())
snap.ref.child('updatedAt').on('child_changed', () => onUpdate(snap))
});
当我更改 UpdatedAt 字段的值时,此侦听器不会触发。
有什么建议欢迎留言!
谢谢!
根据阿里的回答解决方案:
firebase.database().ref('compliments/received/' + currentUser._id).on('child_added', (snap) => {
console.log('add in action', snap.val());
firebase.database().ref('compliments/received/' + currentUser._id)
.child(snap.key).child('updatedAt').on('value', async () => {
const newSnap = await firebase.database().ref('compliments/received/' + currentUser._id)
.child(snap.key).once('value')
onUpdate(newSnap)
})
});