Angularfire: How to set a custom ID when adding a new item into Firebase3

Created on 23 Oct 2016  路  5Comments  路  Source: angular/angularfire

I could not find any doc about how to use angularfire2 for adding a new item with a custom ID to Firebase3.

Does anyone know how to do it ? Please share it with me, thank you!

I have tried the code below but get error: 'child' does not exist on type 'FirebaseListObservable'.

const cachedCart = this.af.database.list('/cachedCart');
cachedCart.child(uid).child(restaurantName).setValue(items)

Most helpful comment

@johnqiuwan I will answer this question here, but please use the issue template in the future. The template makes it much easier for us to answer. Also, these type of questions are best suited for stackoverflow as it is not an issue with the library itself.

In your case you would use FirebaseObjectObservable so you only load the one item.

const cachedCart = this.af.database.object(`/cachedCart/${uid}/${restaurantName}`);
cachedCart.set(items);

You should consider creating a shallow structure. Deeply nested structures are anti-pattern in the Firebase Database as all child data is loaded at the parent path.

All 5 comments

for AutoId keys you can use push() not set()

Hi @FredvanRijswijk ,

Thank you for the reply. However, in my case, I will setup the keys myself (not AutoId).

Does anyone know how to do it with angularfire2 or it is currently NOT possible with angularfire2 ?

@johnqiuwan I will answer this question here, but please use the issue template in the future. The template makes it much easier for us to answer. Also, these type of questions are best suited for stackoverflow as it is not an issue with the library itself.

In your case you would use FirebaseObjectObservable so you only load the one item.

const cachedCart = this.af.database.object(`/cachedCart/${uid}/${restaurantName}`);
cachedCart.set(items);

You should consider creating a shallow structure. Deeply nested structures are anti-pattern in the Firebase Database as all child data is loaded at the parent path.

@davideast Can you say this in Angular js 1 David? using angularFire, Thanks.

Was this page helpful?
0 / 5 - 0 ratings