0

I am using google firebase to store and retrieve data for one of my application. I'm successfully able to push and retrieve the data to and from database. What I'm trying is to set a incremental ID to the document Auto ID of collection.

let db_push = firestore.database().ref('Table').push(); db_push.set({ name:name, age:age, location:location }) 

I'm able to push the data but the ID is setting something like afbghy67kju. And the age data pushing as a string. How can I set the auto numbering ID and push the age var as a number?

2
  • There is no provided auto numbering capability in Realtime Database. You will need to build a solution. If you use a javascript number type, it will show up as a number in the database. If you provide as string, it will appear as a string, even if it looks like a number. Convert the age string to a number first in your code. Commented Apr 16, 2020 at 6:48
  • Yes. I'm able to resolve the number problem. How the ID can be controlled? Is there any way to control it? Commented Apr 16, 2020 at 7:20

1 Answer 1

1

push() will create a random id for every node, if you don't want a random id then you need to add your own implementation. Another way also to use the unique userId instead of push():

firebase.database().ref('Table/' + userId).set({ name:name, age:age, location:location }); 

https://firebase.google.com/docs/database/web/read-and-write

Sign up to request clarification or add additional context in comments.

1 Comment

This actually worked. I have added some logic to generate userId based on no of users. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.