![](/img/trans.png)
[英]Javascript, Iterating through two arrays of objects and adding an item from one to another “if it doesn't exist”
[英]Adding objects to Firebase arrays that don't exist
我正在尝试在Firebase中创建这种数据结构。
"updates": {
"place_id_1": [
"update1": {},
"update2": {}
],
"place_id_2": [
"update1": {},
"update2": {}
]
}
这是我正在使用的代码,但是出现错误TypeError: updates.$child is not a function(…)
。 不确定是什么问题。 如果您尝试添加对象/路径,Firebase是否会创建不存在的对象/路径?
var ref = new Firebase(FirebaseConfig.baseUrl + "/updates");
var updates = $firebaseObject(ref);
updates.$child(update.place_id).$push(update); // will this work?
$child()
和$push
不是AngularFire中的方法。 我强烈建议您阅读AngularFire的快速入门和分步指南 。 在文档中花费几个小时可以避免这里出现数百个问题。
要创建您要查找的结构,可以使用AngularFire,但您不需要它。
使用Firebase JavaScript SDK:
var ref = new Firebase(FirebaseConfig.baseUrl + "/updates");
var place1 = ref.push();
place1.push({ key: 'value1' });
place1.push({ key: 'value2' });
var place2 = ref.push();
place2.push({ key: 'value3' });
place2.push({ key: 'value4' });
使用AngularFire:
var ref = new Firebase(FirebaseConfig.baseUrl + "/updates");
var place1 = $firebaseArray(ref.push());
place1.$add({ key: 'value1' });
place1.$add({ key: 'value2' });
var place2 = $firebaseArray(ref.push());
place2.$add({ key: 'value3' });
place2.$add({ key: 'value4' });
请注意,使用AngularFire读取此类嵌套数组将非常棘手。 这是Firebase文档建议不要使用嵌套数组的原因之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.