2017-10-05 58 views
0

我在firebase上有一个简单的数据库,基本上我试图创建一个代码来在这个数据库上插入记录。我想插入这样的记录:如何使用不同的ID在Firebase数据库中插入项目?

id | lat | lng 
01 | -55 | -10 
02 | -44 | -20 

每个“行”是一个地方(经度和纬度)的新纪录。

我创建了火力这个例子:

enter image description here

我怎么能记录插入到这个“表”正确???因为当我创建了下面的代码来实现它,我打的按钮,它在该表上创建一个不同的ID(我是新来的火力地堡,我没有得到这个尚未):

enter image description here

下面是完整的代码:

<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> 

回答

0

在提交函数中,您没有提供对列表的引用。

var rootRef = new Firebase("https://xxxx.firebaseio.com/").ref(); 

没有列表,这里提供。

var newPlaceRef = rootRef.push(); 
    newPlaceRef.set(data); 

因此,而不是将数据推送到你places清单应用程序中创建了一个自动生成的ID

解决方案

var commentsRef = firebase.database("https://xxxx.firebaseio.com/").ref('places'); 
var newRef = commentsRef.push(); 
newRef .set(data); 

希望这可以帮助您解决您发出一个新的职位的参考。 欲了解更多信息,请阅读官方文档。

Work with Lists of Data on the Web

+0

非常感谢好友。我修改了代码。但是当它创建一个新的“地点”时,它会使用这个自动生成的ID创建。不可能创建一个basica数字自动递增ID? – Reif

+0

这些密钥保证是唯一的,称为推送ID。 –

0

试试这个:

var rootRef = firebase.database().ref('places'); 
var count = 0; // will hold the length of places array 
var value = { lat: +new Date(), lng: +new Date()/1000 }; // sample value for lat and lng 
firebase 
    .database() 
    .ref('places') 
    .once('value', value => { // get the length of your places array 
    if (value.val() == null) count = 0; // if no places then set it to 0 
    else count = Object.keys(value.val()).length; // otherwise get its length 
// as array items are stored corresponding to their index , Object.keys() can give its length 
    }) 
    .then(() => { 
    rootRef.child(count + '').update(value).then(// this will push into your array 
     success => { 
     console.log('success'); 
     }, 
     error => { 
     console.log('error'); 
     } 
    ); 
    }); 

这样,您就可以模仿的Array.push()

您还可以存储places.length在火力本身通过创建一个新的领域。这样,你可以使用该值来代替Object.keys()。每次添加/删除到位数组后,只需保持更新该值即可。

但是你应该使用push()方法。

var data = []; 
var newActivity = { 
     "lat": cityLatToSend, 
     "lng": cityLngToSend, 
     } 
//data.push(newActivity);  don't do this 

rootRef.child('push').push(newActivity) 
//This way you can push into get array . 

希望你明白了。


this

相关问题