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.
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');
}
}
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?
}
}
hi @choopage , you can simply do
const newId = items.push('new item').key();
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.
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:
items.push(yourobject).then((item) => { console.log(item.key); });
@anivaisu @IvRRimum 's answer works. Firebase creates the key for you when you push an item onto a list.
hi,
How can i edit the firebase data using $key
this is my new data from firebase
-KXxxQxJCdo0pidJI1ay
EMAIL:
MOBILE: 654656767
I cannot get the data from firebase when using firebase object $key-->-KXxxQxJCdo0pidJI1ay
kindly advice me
Thanks & regards
@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???
how to store items directly from form using angularfire2
try this way:
this.firebaseApp.database().ref().child('/items'/').push().key;
don't forget import :
import { FirebaseApp } from 'angularfire2';
for version 5, try
const ref = afDb.list('/items).query.ref.push();
ref.set(data);
console.log(ref.key);
for version 5, you can also try:
afDb.list('/items).push(null).then(( ref )=>{
ref.push({/* your_data_here */})
})
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...
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 });
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.
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 createdId() method:
const id = this.afs.createId();
const ref = this.afs.collection(this.collectionRef).doc(id);
hi,
How can i edit the firebase data using $keythis is my new data from firebase
-KXxxQxJCdo0pidJI1ay
EMAIL: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});
Most helpful comment
This works:
items.push(yourobject).then((item) => { console.log(item.key); });