Angularfire2 Associate Firebase Users to Database Records


#1

im new in using angularfire2. i would like to create this database design using ionic and angularfire2

-|items
    -|itemId
        -|user: uid

however there is message that shown that

“runtime error cannot set property ‘userid’ of undefined”

please help me :frowning: here is my code… thank you
here is the link that i learn angularfire angularfire

import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase, FirebaseListObservable } from 'angularfire2/database';

export class Item {
  merknya: string;
  type: string;
  year: string;
  place: string;
  userId : string;
}
@IonicPage()
@Component({
  selector: 'page-input-motor',
  templateUrl: 'input-motor.html',
})
export class InputMotorPage {
  items: FirebaseListObservable<Item[]> = null;
  userId: string;

  constructor(
    private afDatabase: AngularFireDatabase,
    private afAuth: AngularFireAuth,
    public navCtrl: NavController,
    public navParams: NavParams) {
      this.afAuth.authState.subscribe(user =>{
        if(user) this.userId = user.uid })
  }


  ionViewDidLoad() {
    console.log('ionViewDidLoad InputMotorPage');
  }

  getItemsList(): FirebaseListObservable<Item[]> {
     if (!this.userId) return;
     this.items = this.afDatabase.list(`items/`);
     return this.items
  }
  // Create a brand new item
  submit(item: Item)  {
   item.userId = this.userId
   this.items.push(item)
  }
}

#2

It says right at the top of that tutorial that it is out of date. You’ll have a lot more fun if you work with current documentation.


#3

@AaronSterling is right, the most up to date documentation is on GitHub