mercredi 13 septembre 2017

Data keeps on duplicating after adding to database(Firebase database)

I have a table that has starting id of 1. I have a function that gets the last added id using limitToLast(1) and added 1 so that the next data that i will add to the database will have the 2 as the key if ever the last added id is 1.But what happened is this:

THis is whatt happened in my database

It successfully added 2 as the key to the data I added but it continues to add more and duplicates my data. This is my code:

   function uploadProperty() {   

  user = firebase.auth().currentUser;
  var selectedFile;

 var propertiesref = db.ref('property/');
  propertiesref.orderByChild("property").limitToLast(1).on("child_added", 
  function(snapshot) {

   var getkey = Number(snapshot.key);
   var id = getkey + 1;
   firebase.database().ref("property/" + id).set({

     property_id: id,
     user_id: user.uid,
     property_desc: $("#desc").val() , 
     property_address: $("#address0").val() ,    
     property_slot: $("#slot").val(),
     property_price: $("#price").val(),
     rent_type: $("#renttype").val(),
     type: $("#type").val(),
     lat: $("#lat").val(),
     lon: $("#lon").val(),
     date_created: $("#datecreated").val(),  
     image_path: $("#image").val()  

  });

   desc.value = '';   
  address0.value = '';  
  slot.value  = '';
  price.value  = '';
  type.value  = '';
  renttype.value  = '';
  lat.value  = '';
  lon.value  = '';


     });
  }

Aucun commentaire:

Enregistrer un commentaire