我有一些具有以下结构的位置数据。
[business:
Joes Pizza:{
accountid:1818
address:32, Angle des Rue des Nimes et Blvd. Toussaint Louverture,
city:Anytown
country:USA
created:At10/26/2015 7:27:42 PM
heading: Fast Food
headingid: 178
latitude: 18.572203
longitude:-72.306747
name: Joes Pizza
objectId:x9VRotBU2O
phonenumber:1 509 473 6003
website:http://pizza.com
},
]
我正在尝试通过读取单独的纬度和经度信息并使用 push()
编写 latLng
来为所有拥有它的企业重新格式化地理代码信息包含两个键的对象。
我能够创建对象并将其记录到控制台,但是当我尝试在业务对象上调用该对象时,它是未定义的。我通过 firebase 文档中的 set()
和 push()
尝试了这一点。
我尝试了下面的版本以及 fb.child('latLng').push({lat: snapshot.val().latitude, lng: snapshot.val().longitude});
。不能让它去。
<!doctype html>
<html>
<head>
<script src="https://cdn.firebase.com/js/client/2.4.0/firebase.js"></script>
</head>
<body>
<p>Geoloc</p>
<script>
var fb = new Firebase("https://crackling-fire.firebaseio.com/business");
// Retrieve relevant data
fb.on("child_added", function(snapshot) {
var place = snapshot.val();
var latLng = {lat: snapshot.val().latitude, lng: snapshot.val().longitude}
if (place.hasOwnProperty('longitude') && place.hasOwnProperty('latitude'))
{
fb.child('latLng').push(place.latLng);
console.log(place.name, place.latLng);
console.log(latLng);
};
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
</script>
</body>
</html>
最佳答案
我可以看到两个错误:
- 您正在将您的
latLng
设置为根fb
引用的子项 - 您正在使用
push()
来构建一个似乎是命名子节点的结构
这两个的解决方案:
fb.on("child_added", function(snapshot) {
var place = snapshot.val();
if (place.hasOwnProperty('longitude') && place.hasOwnProperty('latitude')) {
snapshot.ref().child('latLng').set({lat: place.latitude, lng: place.longitude});
};
}, function (errorObject) {
console.log("The read failed: " + errorObject.code);
});
关于javascript - 您如何使用 .push 或 set 在 Firebase 中创建新的子 key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35144108/