推火基对象隐藏在唯一键后面,除非使用childAdded,否则很难引用受推对象。如果要在对象上使用update,则需要引用。
{
"ideaCount" : 0,
"ideas" : {
"-KNVuaB6ZaRCLmH0q8ic" : {
"idea" : {
"likes" : 0,
"name" : "test"
}
},
//unique key, every idea will have a new one of these
"-KNVucJEgZxz_N0P8bcv" : {
//want to reference this
"idea" : {
"likes" : 0,
"name" : "jkl"
}
}
}是否有可能更新"idea“对象而不显式地将该唯一键放入引用中?
可以在唯一键之后启动引用吗?根据我的经验,您需要从根开始引用。
更具体地说,我希望能够改变按钮按下的“喜欢”号,而update似乎是最好的方法。
发布于 2016-07-25 12:18:15
简单地回答你的问题,no。您将需要使用push键来完成对ideas/some-idea/likes的引用。但是,我假设您列出了这些“想法”,这样人们就可以喜欢它们了,在其中您将始终拥有可用的push键。例如:
JavaScript
var theIdeas= [];
var ref = firebase.database().ref("ideas");
ref.once("value").then(function(snapshot) {
snapshot.forEach(function(childSnapshot) {
// Push key
var key = childSnapshot.key;
// childData will be the actual contents of the child
var childData = childSnapshot.val();
});
theIdeas.push(childData);
}, function(error) {
});AngularJS
var ref = firebase.database().ref("ideas");
var ideas = $firebaseArray(ref);
<!-- Push key is stored inside $id -->
<div ng-repeat"i in ideas"> {{i.$id}} </div>当你收到这些信息时,你可以随心所欲地去做。要点是,如果您已经掌握了一个“想法”,那么您的确实知道推键是什么,并且应该能够使用它的一部分引用。
参考文献:
https://stackoverflow.com/questions/38562609
复制相似问题