angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

How do we use angularfire2, upon saving/pushing data, to generate Firebase unique id?

Open jek-bao-choo opened this issue 10 years ago • 21 comments

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?

jek-bao-choo avatar Jun 01 '16 12:06 jek-bao-choo

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');
  }
}

davideast avatar Jun 01 '16 12:06 davideast

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?

  }
}

jek-bao-choo avatar Jun 02 '16 01:06 jek-bao-choo

hi @choopage , you can simply do

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

kazkis avatar Jun 02 '16 21:06 kazkis

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.

jek-bao-choo avatar Jun 03 '16 00:06 jek-bao-choo

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.

jongood01 avatar Jul 18 '16 21:07 jongood01

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

IvRRimum avatar Sep 06 '16 14:09 IvRRimum

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

IvRRimum avatar Sep 06 '16 20:09 IvRRimum

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

jongood01 avatar Dec 01 '16 20:12 jongood01

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

anivaisu avatar Dec 02 '16 05:12 anivaisu

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

jesseteweehi avatar Dec 20 '16 19:12 jesseteweehi

how to store items directly from form using angularfire2

AnuragKDwivedi avatar Dec 23 '16 09:12 AnuragKDwivedi

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

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

RikaonPessoa avatar Aug 26 '17 14:08 RikaonPessoa

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

wor-k avatar Oct 07 '17 03:10 wor-k

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

flaviuj avatar Oct 08 '17 16:10 flaviuj

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

paean99 avatar Nov 28 '17 22:11 paean99

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 });

cwoolum avatar Dec 10 '17 04:12 cwoolum

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.

kanafghan avatar Feb 07 '18 10:02 kanafghan

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

paean99 avatar Feb 07 '18 12:02 paean99

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.

PVermeer avatar Mar 07 '19 13:03 PVermeer

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

yuliankarapetkov avatar Jul 17 '19 10:07 yuliankarapetkov