Search code examples
firebasefirebase-realtime-databaseangularfire2angularfire5

Getting reference to an item using AngularFire2


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?


Solution

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