1
votes

im new using firebase, im working in ionic and Firebase project using angularfire. i would like to create this database

-|items
  -|itemId
    -|user: uid

however, the code is not working. there is no error message, but it fail to push data to database. here is my code

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

import { AngularFireAuth } from 'angularfire2/auth';
import { AngularFireDatabase, firebaseListObservable } from 'angularfire2/database';
import { Item } from '../../models/item';
import { CustomerHomePage } from '../customer-home/customer-home';

// 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 {
item = {} as Item ;
inputItem : firebaseListObservable<Item>=[] ;
userId : string ;

  constructor(
    private afDatabase: AngularFireDatabase,
    private afAuth: AngularFireAuth,
    public navCtrl: NavController,
    public navParams: NavParams) {
      this.afAuth.authState.take(1).subscribe(auth => {
      this.userId= this.afAuth.auth.currentUser.uid
    });
  }


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

  getItem():firebaseListObservable<Item>{
  this.inputItem = this.afDatabase.list('Item/' ).valueChanges();
    return this.inputItem
}

  // Create a brand new item
  submit(item : Item) {
       this.item.userId = this.userId
       this.inputItem.push(this.item)
  }
}

would you guys help me solve my problem. thank you very much

1

1 Answers

0
votes

Look at the changes above. You need to distinguish between AngularFireList & observable:

 fbList :any;
     constructor(
        private afDatabase: AngularFireDatabase,
        private afAuth: AngularFireAuth,
        public navCtrl: NavController,
        public navParams: NavParams) {
          this.afAuth.authState.take(1).subscribe(auth => {
          this.userId = this.afAuth.auth.currentUser.uid
        });
        this.fbList = this.afDatabase.list('items/' )
      }


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

      getItem():firebaseListObservable<Item>{
      this.inputItem = this.fbList.valueChanges();
        return this.inputItem
    }

      // Create a brand new item
      submit(item : Item) {
           this.item.userId = this.userId
           this.fbList.push(this.item)
      }