1
import { Injectable } from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
/*
  Generated class for the FirebaseService provider.

  See https://angular.io/docs/ts/latest/guide/dependency-injection.html
  for more info on providers and Angular 2 DI.
*/
@Injectable()
export class FirebaseService {

  users:FirebaseListObservable<any>;

  constructor(public angularFire: AngularFire,) {
    console.log('Hello FirebaseService Provider');
    this.users=this.angularFire.database.list('users');
}

  saveuser(user){
    this.users.push(user);
  }

}

hi there in this code i'm trying to add new user to my firebase table under the users table the problem is i can't find the set method i only have access to push is it something i'm doing wrong ? thank you for the help

2 Answers 2

2

AngularFire2 does not expose set for list observables. You should use update instead.

This makes some sense when you look at the object observable, which does expose set. When set is called on the object observable, the entire content of the database object is replaced with the specified value.

Typically, you won't ever want to replace the entire list, so it makes some sense for set to not be exposed. And update is appropriate, as you are updating the list be adding a new user.

this.users.update(user.id /* or whatever is to be the key */, user);

Alternatively, you could use the list observable's SDK ref to call set:

this.users.$ref.ref.child(user.id /* or whatever is to be the key */).set(user);
0

Try the following instead as an example case, which:

list.$add({ foo: "bar" }).then(...);

This example is taken from the AngularFire documentation.

Not the answer you're looking for? Browse other questions tagged or ask your own question.