javascript - Data keeps on duplicating after adding to database -
i have table has starting id of 1. have function gets last added id using limittolast(1) , added 1 next data add database have 2 key if ever last added id 1.but happened this:
it added 2 key data added continues add more , duplicates data. 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 = ''; }); }
you should using once() instead of on(). on()
leaves listener attached, when store new child with:
firebase.database().ref("property/" + id).set(...);
the listener fires again.
Comments
Post a Comment