Skip to content

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

Closed
jek-bao-choo opened this issue Jun 1, 2016 · 21 comments

Comments

@jek-bao-choo
Copy link
Contributor

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.

  1. How do we do this using AngularFire2?
  2. Or maybe should we be asking is there a need to use Firebase Unique Id in the first place?
  3. What is a good self generated unique id when using it with Firebase? GUID?
@davideast
Copy link
Collaborator

Hey @choopage!

You can generate the unique id, which we call a "push id", using the .push() method.

class MyCmp
  constructor(af: AngularFire) {
    const items = af.database.list('items');
    items.push('new item');
  }
}

@jek-bao-choo
Copy link
Contributor Author

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

class MyCmp
  constructor(af: AngularFire) {
    const items = af.database.list('items');
    items.push('new item').then(... $key ...);

//is there a way to get the 'push id' right after the push operation?

  }
}

@kazkis
Copy link

kazkis commented Jun 2, 2016

hi @choopage , you can simply do

const newId = items.push('new item').key();

@jek-bao-choo
Copy link
Contributor Author

Hi @kazkis,

Thank you for your answer. Perfect answer for me moving forward.

Sent from my iPhone

On 3 Jun 2016, at 05:21, kazkis [email protected] wrote:

hi @choopage , you can simply do

const newId = items.push('new item').key();


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub, or mute the thread.

@jongood01
Copy link

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.

@IvRRimum
Copy link

IvRRimum commented Sep 6, 2016

Then how do you get the key? Why did you change such a good approach?

@IvRRimum
Copy link

IvRRimum commented Sep 6, 2016

This works:
items.push(yourobject).then((item) => { console.log(item.key); });

@jongood01
Copy link

@anivaisu @IvRRimum 's answer works. Firebase creates the key for you when you push an item onto a list.

@anivaisu
Copy link

anivaisu commented Dec 2, 2016

hi,
How can i edit the firebase data using $key

this is my new data from firebase

-KXxxQxJCdo0pidJI1ay
EMAIL:

FIRSTNAME:john

LASTNAME:david

MOBILE: 654656767

I cannot get the data from firebase when using firebase object $key-->-KXxxQxJCdo0pidJI1ay

kindly advice me

Thanks & regards

@jesseteweehi
Copy link

@jongood01

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???

@AnuragKDwivedi
Copy link

how to store items directly from form using angularfire2

@RikaonPessoa
Copy link

try this way:
this.firebaseApp.database().ref().child('/items'/').push().key;

don't forget import :
import { FirebaseApp } from 'angularfire2';

@wor-k
Copy link

wor-k commented Oct 7, 2017

for version 5, try
const ref = afDb.list('/items).query.ref.push(); ref.set(data); console.log(ref.key);

@flaviuj
Copy link

flaviuj commented Oct 8, 2017

for version 5, you can also try:
afDb.list('/items).push(null).then(( ref )=>{
ref.push({/* your_data_here */})
})

@paean99
Copy link

paean99 commented Nov 28, 2017

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.

const newId = items.push('new item').key();

edit: This one and all the earlier solutions are really the same one in different clothes...

@cwoolum
Copy link

cwoolum commented Dec 10, 2017

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.

let receiptRef = this.receipts.push(receiptValue);
receiptRef.update({ id: receiptRef.key });

@kanafghan
Copy link

For me, having the same case as @cwoolum , the following works as of 5.0.0-rc.6:

const pushId = this.afDb.createPushId();
const item = { ...item, id: pushId };

this.afDb.list('items').set(item.id, item);

AngularFireDatabase has the createPushId() method which is not mentioned anywhere the documentation.

@paean99
Copy link

paean99 commented Feb 7, 2018

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).

@PVermeer
Copy link

PVermeer commented Mar 7, 2019

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.

@yuliankarapetkov
Copy link

As mentioned in this post you can use the createdId() method:

const id = this.afs.createId();
const ref = this.afs.collection(this.collectionRef).doc(id);

@Shashidhar1998
Copy link

hi,
How can i edit the firebase data using $key

this is my new data from firebase

-KXxxQxJCdo0pidJI1ay
EMAIL:

FIRSTNAME:john

LASTNAME:david

MOBILE: 654656767

I cannot get the data from firebase when using firebase object $key-->-KXxxQxJCdo0pidJI1ay

kindly advice me

Thanks & regards

store key value in object user
this.firebase.object('/Users/' + user.$key)
.update({ username: user.username,email:user.email});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests