-
Notifications
You must be signed in to change notification settings - Fork 2.2k
How do we use angularfire2, upon saving/pushing data, to generate Firebase unique id? #199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hey @choopage! You can generate the unique id, which we call a "push id", using the class MyCmp
constructor(af: AngularFire) {
const items = af.database.list('items');
items.push('new item');
}
} |
Heyy @davideast Thank you for answering my question. "Push id" is what I am looking for. Just before we close this issue, could you explain following your code sample, how do we get the "push id" after doing a push? Just in case, you are wondering why I'm doing this. I want to instantly using the push id returned from the push operation to perform routing to localhost:4200/hero/-KJDdFcQzEmyOhAHjICx
|
hi @choopage , you can simply do
|
Hi @kazkis, Thank you for your answer. Perfect answer for me moving forward. Sent from my iPhone
|
This no longer works as the push method returns a promise. Using preserveSnapshot on the list also doesn't seem to be ideal when you want to make sure you get the key of the newly created item. |
Then how do you get the key? Why did you change such a good approach? |
This works: |
hi, this is my new data from firebase -KXxxQxJCdo0pidJI1ay FIRSTNAME:john LASTNAME:david MOBILE: 654656767 I cannot get the data from firebase when using firebase object $key-->-KXxxQxJCdo0pidJI1ay kindly advice me Thanks & regards |
With the answer from @IvRRimum if i use this approach and use the key to update on another location like Updating or deleting data will they be atomic??? |
how to store items directly from form using angularfire2 |
try this way: don't forget import : |
for version 5, try |
for version 5, you can also try: |
For "version": "5.0.0-rc.4", this works. const newKey = afDb.list('/items').push(newItem).key Remark that it isn't a method like in the solution mentioned earlier.
edit: This one and all the earlier solutions are really the same one in different clothes... |
I ended up going this route... It seemed the cleanest for what I was trying to do. I didn't like pushing the null to the collection because you can't use validations in your DB config. This is for 5.0.0-rc4.
|
For me, having the same case as @cwoolum , the following works as of 5.0.0-rc.6:
|
Yes, createPushId() is also what i have been using. It promotes a cleaner workflow. I only found it initially because of the intellisense of the editor. I did search the documentation for it, without success. But the earlier solutions still have its necessity if one specifically need the key only after the push (for whatever the reason). |
Is there anymore info on createPushId()? It types as createPushId(): string | null; In what case can it be null? I cannot find anything in the src. |
As mentioned in this post you can use the
|
store key value in object user |
I'm aware that using Firebase REST we could POST and have a Firebase unique id generated. For example, a id that looks like this, -Kbz13Uo3.
The text was updated successfully, but these errors were encountered: