从更改快照密钥中检索特定数据



我正在尝试从一个不断更新的孩子的Firebase数据中检索JSON数据。快照密钥在每次新数据提交时都会更改。(使用 SparkPost 将数据发送到 Firebase(。我正在尝试找出一种方法来构建我的检索,以便它从最新的密钥中获取。

var ref = new Firebase("https://watspark.firebaseio.com/raw-events/");
var _url = ('https://watspark.firebaseio.com/raw-events/');

// Retrieve new posts as they are added to our database
ref.on("child_added", function(snapshot, prevChildKey) {
  var newPost = snapshot.val();
  console.log(snapshot.val());
  // console.log("text: "  + newPost.text);
  // console.log("Title: " + newPost.title);
  // console.log("Previous Post ID: " + prevChildKey);
  // var fireUrl = ("https://watspark.firebaseio.com/raw-events/" + snapshot.val() + "/0/msys/relay_message/content");
  //  var bigBoy = new Firebase(fireUrl);
  //   console.log("text: " + bigBoy.text);
  var param = snapshot.key();
  console.log(param);
});
ref.orderByChild("0/mysys/relay_message/content/text").on("child_added",   function(snapshot) {
  console.log(snapshot.key() + "was" + snapshot.val().text);
});

我的 JSON 数据结构如下:

   snapshot.val();
   {
     0 {
       mysys{
         relay_message{
           content

我将如何做到这一点?谢谢

我可以通过这样做来调用最近的孩子:

ref.orderByKey().limitToLast(1).on("child_added", function(snapshot) {
  var child = snapshot.val();
  console.log(snapshot.key());
  var snapKey = '' + snapshot.key();
  snapshot.child("0/msys/relay_message/content/").forEach(function(snap){
    console.log('snap ' + snap.val());
    if(snap.key() === 'text'){
      watsonInput = snap.val();
    }
  });
  snapshot.child("0/msys/relay_message/").forEach(function(snap){
    console.log('email: ' + snap.val());
    if(snap.key() === 'msg_from'){
      email = snap.val();
    }
    //call to remove current snapshotKey
  });

相关内容

  • 没有找到相关文章

最新更新