将对象添加到不存在的 Firebase 数组
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 会创建 objects/paths 吗?
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 的 quickstart and step-by-step guide。在文档中花费几个小时可以避免这里出现数百个问题。
要创建您正在寻找的结构,您可以使用 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 文档建议不要嵌套数组的原因之一。
我正在尝试在 Firebase 中创建这种数据结构。
"updates": {
"place_id_1": [
"update1": {},
"update2": {}
],
"place_id_2": [
"update1": {},
"update2": {}
]
}
这是我正在使用的代码,但出现错误 TypeError: updates.$child is not a function(…)
。不确定是什么问题。当您尝试添加它们时,如果它们不存在,Firebase 会创建 objects/paths 吗?
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 的 quickstart and step-by-step guide。在文档中花费几个小时可以避免这里出现数百个问题。
要创建您正在寻找的结构,您可以使用 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 文档建议不要嵌套数组的原因之一。