简体   繁体   English

Firebase 实时数据库访问来自已删除节点的数据数组

[英]Firebase real time database access data array from deleted node

I am deleting a FRTDB node, I want to access deleted data from that node.我正在删除一个 FRTDB 节点,我想从该节点访问已删除的数据。 the functions looks as follow:功能如下:

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;
  }
})

The probem I am having is that我遇到的问题是

admin.database().ref(`/events/${eventId}/dayofweek admin.database().ref(`/events/${eventId}/dayofweek

do not loop the data because it seems data is no longer there so the forEach is not working.不要循环数据,因为似乎数据不再存在,所以 forEach 不起作用。 How can I get access to this data and get to loop the deleted data?如何访问这些数据并循环删除已删除的数据?

Of course you won't be able to read data that was just deleted.当然,您将无法读取刚刚删除的数据。 The function runs after the delete is complete.该函数删除完成运行。 If you want to get the data that was just deleted, you're supposed to use change.before as described in the documentation :如果您想获取刚刚删除的数据,您应该按照文档中的描述使用change.before

The Change object has a before property that lets you inspect what was saved to Realtime Database before the event. Change 对象具有before属性,可让您检查事件发生前保存到实时数据库的内容。 The before property returns a DataSnapshot where all methods (for example, val() and exists()) refer to the previous value. before 属性返回一个 DataSnapshot,其中所有方法(例如,val() 和 exists())都引用前一个值。 You can read the new value again by either using the original DataSnapshot or reading the after property.您可以通过使用原始 DataSnapshot 或读取after属性再次读取新值。 This property on any Change is another DataSnapshot representing the state of the data after the event happened.任何 Change 上的此属性是另一个 DataSnapshot,表示事件发生后数据的状态。

The data that was deleted from the database is actually included in the call to your Cloud Function.从数据库中删除的数据实际上包含在对 Cloud Function 的调用中。 You can get if from change.before .你可以从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;
    ...
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM