Firebase - 推送推送数据

时间:2015-03-20 19:36:51

标签: javascript firebase

这是我的数据

{
  "deck" : {
    "-JkpwAnieKjQVsdtPD4m" : {
      "deck" : "Deck 1",
      "user" : "simplelogin:1"
    },
    "-Jkq4unexm-qwhO_U2YO" : {
      "deck" : "Deck 2",
      "user" : "simplelogin:1"
    },
    "-Jkq5-II1q5yM6w3ytmG" : {
      "deck" : "Deck 3",
      "user" : "simplelogin:6"
    },
    "-Jks5mbMHmPB9MwnnOCj" : {
      "deck" : "Deck 4",
      "user" : "simplelogin:1"
    }
  }
}

如果我想添加:

      cards: {
        "-GeneratedKey":{
          "title":"foo",
          "text":"bar",
        }
      }

用甲板和#34;甲板2"来说明项目,如何选择要推送的对象。最终结果将是:

{
  "deck" : {
    "-JkpwAnieKjQVsdtPD4m" : {
      "deck" : "Deck 1",
      "user" : "simplelogin:1"
    },
    "-Jkq4unexm-qwhO_U2YO" : {
      cards: {
        "-GeneratedKey":{
          "title":"foo",
          "text":"bar",
        }
      }
      "deck" : "Deck 2",
      "user" : "simplelogin:1"
    },
    "-Jkq5-II1q5yM6w3ytmG" : {
      "deck" : "Deck 3",
      "user" : "simplelogin:6"
    },
    "-Jks5mbMHmPB9MwnnOCj" : {
      "deck" : "Deck 4",
      "user" : "simplelogin:1"
    }
  }
}

以下是我的尝试:

  deckRef.orderByChild('deckName').equalTo('Deck 2').push({
    card: {
      title: 'foo',
      text: 'bar'
    }
  });

但是刚刚返回错误。我该怎么做才能实现这个目标?

1 个答案:

答案 0 :(得分:2)

deckRef.orderByChild('deckName').equalTo('Deck 2')返回一个查询,而不是ref。查询可以匹配许多节点。即使在你的情况下它只匹配一个,你需要首先将一个节点捕获到一个参考,以便能够push到它。

var query = deckRef.orderByChild('deckName').equalTo('Deck 2');
query.once('child_added', function(snapshot) {
    snapshot.ref().child('cards').push({
        title: 'foo',
        text: 'bar'
    });
});