Auto increment value in firebase using javascript

Hello, I am working on a firebase project, and I can save my data through javascript in a firebase database. But I could not figure out how to automatically increase the child value (my child value is duyuru, you can see the details below) of my database. I am sharing my code below, you can give me hints to solve this problem.

<script> // Initialize Firebase var config = { apiKey: "sample1", authDomain: "sample2", databaseURL: "sample3", storageBucket: "sample4", }; firebase.initializeApp(config); var database = firebase.database(); firebase.initializeApp(config); var database = firebase.database(); function writeUserData(userId, name, email, imageUrl) { var kategori1 = document.getElementById("kategori").value; var duyuru1 = document.getElementById("duyuru").value; var name1 = document.getElementById("name").value; var email1 = document.getElementById("email").value; var imageUrl1 = document.getElementById("imageUrl").value; firebase.database().ref(kategori1 +"/" + duyuru1).set({ username: name1, email: email1, profile_picture : imageUrl1 }); } </script> 

and the output is as follows

 { "duyuru1": { "email": "kjfdlkl", "profile_picture": "dsfsd", "username": "meraha" } } 

I want to output such data:

 { "duyuru1": { "email": "kjfdlkl", "profile_picture": "dsfsd", "username": "meraha" }, "duyuru2": { "email": "kjfdlkl", "profile_picture": "dsfsd", "username": "meraha" } } 

The duyuru (child value) section should be auto incremented, this is what I wanted. thank you for your responses

+5
source share
1 answer

Firebase does not have automatic incremental keys, as they do not work well in multi-user systems, where clients can be offline for extended periods of time.

Instead, Firebase has its own type of automatically generated key called push ID. These push identifiers share the same important sequence properties in many relational databases: they are ordered and sequential. But, in addition, they can be calculated on the client side, even if the client is not connected to Firebase servers.

See the Firebase documentation for storing data in lists , this is a legacy of the blog post about why arrays don't work well in Firebase , and this is a post on how push files work .

+8
source

All Articles