Angularfire: Clarification: How to add an item without loading the entire list?

Created on 25 Jun 2016  路  3Comments  路  Source: angular/angularfire

So with everything being relative to the root path I'm struggling a bit, but I'm also wondering if when I call for a list if I'm loading all the items, and if so how do I just get the ref?

Consider a path of: https://my-site.firebaseapp.com/projects/<projectId>/data/logs

First I'm struggling with having to use the full string path where before I could just have a project service and say currentProjectRef = projectsRef.child('projectId')
because AngularFire doesn't expose the ref for me.

Second when I call
this.logs:any = this.af.database.list('https://my-site.firebaseapp.com/projects/<projectId>/data/logs');

Does this automatically pull the ENTIRE node? I just want to point to it, not list it.. Consider the function:

addLogItem(item:any) {
    this.logs.push(item);
}

I don't want to display all the logs, I just want to add to them. Does just calling "list" retrieve them or does it act as a reference unless subscribed to?

If it does load them, how can I just get the reference?

Most helpful comment

You can just use a regular database reference. The point of the AngularFireDatabase class is to synchronize collections. If you're need is just to add items without loading any then just call .set() or .push() on a regular reference.

const myRef = firebase().database().ref().child("items");
myRef.push({ name: 'item' });

All 3 comments

You can just use a regular database reference. The point of the AngularFireDatabase class is to synchronize collections. If you're need is just to add items without loading any then just call .set() or .push() on a regular reference.

const myRef = firebase().database().ref().child("items");
myRef.push({ name: 'item' });

@davideast is this answer still valid? how do we add an item without loading the entire list with latest version of angularfire2. currently i am doing this :
`
addTag(tag: TagModel) {

const d = this.af.database.list('/tags');

d.push(tag)

  .then((i) => { console.log(i.key); }, (err) => {console.log(err); });

}
`

Hi @pinalbhatt, no sure if this helps or not, but what I did is just specify the child key I want to insert under

For example:

private db: AngularFireDatabase; // definition for db
this.db.object('/tags/childKey').set({ tag })

Hope that helps

Was this page helpful?
0 / 5 - 0 ratings