使用Firebase监听嵌套更改的正确方法是什么? [英] What is the correct way to listen to nested changes using Firebase?

查看:81
本文介绍了使用Firebase监听嵌套更改的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

后台:

我正在尝试使用Firebase,Twilio和Node.js通过浏览器发送短信。我在Firebase中的当前数据结构如下所示:

I'm trying to send SMS messages via the browser using Firebase, Twilio, and Node.js. My current data structure in Firebase looks like this:

{ messages :
  { +15553485 :
    { FB-GENERATED-KEY-1 :
      { body: "hello world"
        timestamp: 1461758765472 }
    },
    { FB-GENERATED-KEY-3 :
      { body: "I love dogs"
        timestamp: 1461758765475 }
      }
    }
  },
  { +15550000 :
    { FB-GENERATED-KEY-2 :
      { body: "goodbye world"
        timestamp: 1461758765473 }
    },
    { FB-GENERATED-KEY-4 :
      { body: "I love cats"
        timestamp: 1461758765476 }
      }
    }
  }
}

当消息通过前端添加到Firebase时,后端需要得到通知才能通过Twilio发送短信。当后端从手机获得回复时(通过Twilio),它会将其添加到Firebase。

When a message is added to Firebase via the frontend the backend needs to get notified in order to send an SMS via Twilio. When the backend gets a reply from the phone (via Twilio), it adds it to Firebase.

问题:

当我收听线程的更改时,我会收到针对该电话号码发送/接收的所有消息。显然后端不想再发送所有消息,所以我只对添加到帖子中的最新消息感兴趣。

When I listen for changes to a thread I receive all messages sent/recieved for that phone number. Obviously the backend doesn't want to send all the messages again, so I'm only interested in the most recent message added to the thread.

我也不能似乎很容易得到其下面有消息的电话号码(密钥)。

Also I can't seem to easily get the phone number (the key) that has messages underneath it.

我尝试了什么:

ref.child('messages')。on('child_added',...) - 这适用于新的电话号码在 / messages 添加,但Firebase不会通过新的电话号码(密钥)发送,只有来自 FB-GENERATED-KEY-2的所有内容 down。

ref.child('messages').on('child_added', ...) — this works for new phone numbers that are added at /messages, however Firebase doesn't send through the new phone number (key), only everything from FB-GENERATED-KEY-2 down.

ref.child('messages')。on('child_changed',...) - 这将返回线程中消息的所有,而不仅仅是新消息。我可以在服务器上排序并找到最新的消息,但这似乎会很快变得很重 - 如果你发送了数千条消息怎么办?

ref.child('messages').on('child_changed', ...) — this returns all of the messages in a thread, not only the new ones. I can sort on the server and find the most recent message, but that seems like it'll get heavy quite quickly – what if you've sent thousands of messages?

在根级别存储消息(也就是说,展平树)并将数字存储为属性可能会起作用,但是我将需要使用电话号码作为一种索引以便稍后与其他数据连接(如外键)。

Storing messages at the root level (aka. flattening the tree) and storing the number as an attribute instead could work, but I'm going to need to use the phone number as a sort of index to connect with other data later (like a foreign key).

问题:


  • 如何在收听父 / messages 上的活动而不是特定的电话号码时才能收到最新消息?

  • 如何使用 child _ 事件时,我可以获得密钥(电话号码)吗?

  • 此数据结构是否有意义?

  • How can I only get the most recent message when listening to activity on the parent /messages and not a particular phone number?
  • How can I get the key (phone number) when using a child_ event?
  • Does this data structure make sense?

推荐答案

您可以通过拨打 key() 你的 child_added 监听器返回hot。

You can get the Firebase key by calling key() on the snapshot returned by your child_added listener.

然后你可以添加另一个嵌套的监听器:

Then you can add another nested listener like this:

ref.child('messages').on('child_added', function (snapshot) {
    var phone = snapshot.key();
    ref.child('messages').child(phone).on('child_added', function (message) {
        //send SMS
    }, function (error) {

    });
}, function (error) {

});

这篇关于使用Firebase监听嵌套更改的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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