Firebase 实时数据库从已删除节点访问数据数组
Firebase real time database access data array from deleted node
我正在删除一个 FRTDB 节点,我想从该节点访问已删除的数据。函数如下所示:
exports.events = functions.database.ref('/events/{eventId}').onWrite(async (change, context) => {
const eventId = context.params.eventId
if (!change.after.exists() && change.before.exists()) {
//data removed
return Promise.all([admin.database().ref(`/events/${eventId}/dayofweek`).once('value')]).then(n => {
const pms = []
const days = n[0]
days.forEach(x => {
pms.push(admin.database().ref(`${change.before.val().active ? 'active' : 'inactive'}/${x.key}/${eventId}`).set(null))
})
return Promise.all(pms)
});
else {
return null;
}
})
我遇到的问题是
admin.database().ref(`/events/${eventId}/dayofweek
不要循环数据,因为数据似乎不再存在,所以 forEach 无法正常工作。我怎样才能访问这些数据并循环删除的数据?
当然,您将无法读取刚刚删除的数据。该函数在 删除完成后 运行。如果你想获得刚刚删除的数据,你应该使用 change.before
中描述的 documentation:
The Change object has a before
property that lets you inspect what was
saved to Realtime Database before the event. The before property
returns a DataSnapshot where all methods (for example, val() and
exists()) refer to the previous value. You can read the new value
again by either using the original DataSnapshot or reading the after
property. This property on any Change is another DataSnapshot
representing the state of the data after the event happened.
从数据库中删除的数据实际上包含在对您的 Cloud Function 的调用中。你可以从 change.before
.
得到 if
exports.events = functions.database.ref('/events/{eventId}').onWrite(async (change, context) => {
const eventId = context.params.eventId
if (!change.after.exists() && change.before.exists()) {
//data removed
days = change.before.val().dayofweek;
...
})
我正在删除一个 FRTDB 节点,我想从该节点访问已删除的数据。函数如下所示:
exports.events = functions.database.ref('/events/{eventId}').onWrite(async (change, context) => {
const eventId = context.params.eventId
if (!change.after.exists() && change.before.exists()) {
//data removed
return Promise.all([admin.database().ref(`/events/${eventId}/dayofweek`).once('value')]).then(n => {
const pms = []
const days = n[0]
days.forEach(x => {
pms.push(admin.database().ref(`${change.before.val().active ? 'active' : 'inactive'}/${x.key}/${eventId}`).set(null))
})
return Promise.all(pms)
});
else {
return null;
}
})
我遇到的问题是
admin.database().ref(`/events/${eventId}/dayofweek
不要循环数据,因为数据似乎不再存在,所以 forEach 无法正常工作。我怎样才能访问这些数据并循环删除的数据?
当然,您将无法读取刚刚删除的数据。该函数在 删除完成后 运行。如果你想获得刚刚删除的数据,你应该使用 change.before
中描述的 documentation:
The Change object has a
before
property that lets you inspect what was saved to Realtime Database before the event. The before property returns a DataSnapshot where all methods (for example, val() and exists()) refer to the previous value. You can read the new value again by either using the original DataSnapshot or reading theafter
property. This property on any Change is another DataSnapshot representing the state of the data after the event happened.
从数据库中删除的数据实际上包含在对您的 Cloud Function 的调用中。你可以从 change.before
.
exports.events = functions.database.ref('/events/{eventId}').onWrite(async (change, context) => {
const eventId = context.params.eventId
if (!change.after.exists() && change.before.exists()) {
//data removed
days = change.before.val().dayofweek;
...
})