1

I'm using it in react native app to save the purchase history in firebase with only 0,1,2,3,4.... keys not the system generated unique key

enter image description here

I'm using this below syntax

firebase2.database().ref('user_data/' + my_key+"/purchase_history").push(MY_DATA) 

I want to remove the key between purchase_history and 0 and when I add the new data then it should be visible like 0 then 1 then 2 then 3 and so on......

1 Answer 1

2

If I correctly understand your question, you should use the set() method instead of the push() one, as follows:

const newKey = "0"; // Or "1", "2", "3".... firebase2.database().ref("user_data/" + my_key + "/purchase_history/" + newKey).set(MY_DATA); 

As a matter of fact, push() generates a new child location using a unique key which is automatically generated by the Realtime Database (i.e. -MR99pAG....).

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

6 Comments

Thanks for answer but In case of adding more data then it will always rewrite the 0th value again!
It's up to you to generate an incremental value for the key. There are only two options: either you use the auto-generated key with push() but it will have the format we see in the screenshot, i.e. -MR99pAG.... (it is based on a timestamp) OR you generate yourself the key because you want to have a specific format (e.g. 1, 2, 3...). In the second case, you have the full control. Therefore, if you want to have a sequence like 1, 2, 3 you need to generate it.
Yes I just want the same 0,1,2,3.... can you tell me how to generate this keys like 0123
"can you tell me how to generate this keys like 0123" => I cannot answer this question, it highly depends on your overall use case. It could be a simple loop to increment a variable. The key is just a simple string or number.
ok and how I get previous key of value I means how do I get 0,1,2,3 of previous used
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.