我在firebase上有一个简单的数据库,基本上我试图创建一个代码来在这个数据库上插入记录。我想插入这样的记录:如何使用不同的ID在Firebase数据库中插入项目?
id | lat | lng
01 | -55 | -10
02 | -44 | -20
每个“行”是一个地方(经度和纬度)的新纪录。
我创建了火力这个例子:
我怎么能记录插入到这个“表”正确???因为当我创建了下面的代码来实现它,我打的按钮,它在该表上创建一个不同的ID(我是新来的火力地堡,我没有得到这个尚未):
下面是完整的代码:
<form action="" id="newActivity">
<input id="searchTextField" type="text" size="50" placeholder="Enter a location" autocomplete="on" runat="server" />
<input type="hidden" id="city2" name="city2" />
<input type="hidden" id="cityLat" name="cityLat" />
<input type="hidden" id="cityLng" name="cityLng" />
<input id="saveForm" name="saveForm" type="submit" value="New Activity">
</form>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.firebase.com/js/client/2.3.2/firebase.js"></script>
<script src="http://maps.googleapis.com/maps/api/js?libraries=places" type="text/javascript"></script>
<script type="text/javascript">
function initialize() {
var input = document.getElementById('searchTextField');
var autocomplete = new google.maps.places.Autocomplete(input);
google.maps.event.addListener(autocomplete, 'place_changed', function() {
var place = autocomplete.getPlace();
document.getElementById('city2').value = place.name;
document.getElementById('cityLat').value = place.geometry.location.lat();
document.getElementById('cityLng').value = place.geometry.location.lng();
//alert("This function is working!");
//alert(place.name);
// alert(place.address_components[0].long_name);
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
<script>
$(function(){
var rootRef = new Firebase("https://xxxx.firebaseio.com/").ref();
var placesRef = rootRef.child('places');
/**
* Data object to be written to Firebase.
*/
var data = []
$('#newActivity').submit(function(event){
var $form = $(this);
console.log("submit to Firebase");
//make the submit disabled
//$form.find("#saveForm").prop('disabled', true);
//get the actual values that we will send to firebase
var cityLatToSend = $('#cityLat').val();
console.log(cityLatToSend);
var cityLngToSend = $('#cityLng').val();
console.log(cityLngToSend);
//take the values from the form, and put them in an object
var newActivity = {
"lat": cityLatToSend,
"lng": cityLngToSend,
}
//put the new object into the data array
data.push(newActivity);
console.log(data);
//send the new data to Firebase
var newPlaceRef = rootRef.push();
newPlaceRef.set(data);
//rootRef.push(data);
return false;
})
})
</script>
非常感谢好友。我修改了代码。但是当它创建一个新的“地点”时,它会使用这个自动生成的ID创建。不可能创建一个basica数字自动递增ID? – Reif
这些密钥保证是唯一的,称为推送ID。 –