1

I am using AF2 v5 and using the Real-time database.

I want to get the reference to a node or item in my data. After checking the docs I found the following.

const listRef = db.list('items');

Notice the user of the .list() method. The return type of the above statement is AngularFireList{[]}.

I was hoping to get the return type of Reference.

Is this the correct way to get a reference to a node so that I can perform CRUD to it?

krv
  • 2,830
  • 7
  • 40
  • 79

1 Answers1

1

You need to use db.object() to get a single firebase.database.Reference.

const item = db.object('items/itemID').valueChanges();

Check the official doc

You can perform the CRUD like

const itemRef = db.object('items/itemID');

itemRef.remove();

itemRef.set({ name: 'new name!'});

itemRef.update({ age: newAge });
Hareesh
  • 6,770
  • 4
  • 33
  • 60
  • I don't know why you would call this a `DocumentReference`. The return value from `db.object` is `AngularFireObject`, right? I think the OP (although he does say he wants to do CRUD on it), or at least I, wants to get the `firebase.database.Reference` corresponding to the `AngularFireObject` or `AngularFireList`. –  Jun 05 '18 at 03:40
  • @torazaburo you are right. i mixed my head with `firestore` database. It should be `firebase.database.Reference`. And `AngularFireObject` is just a wrapper around `firebase.database.Reference` [link](https://github.com/angular/angularfire2/blob/master/src/database/interfaces.ts). – Hareesh Jun 06 '18 at 12:58