angularfire2 firebaseListObservable推送数据

时间:2018-04-20 21:09:15

标签: javascript firebase firebase-realtime-database ionic3 angularfire2

im new using firebase,我使用angularfire在离子和Firebase项目中工作。我想创建这个数据库

-|items
  -|itemId
    -|user: uid

但是,代码无效。没有错误消息,但无法将数据推送到数据库。这是我的代码

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)
  }
}
你能帮助我解决我的问题。非常感谢你

1 个答案:

答案 0 :(得分:0)

查看上面的更改。你需要区分AngularFireList&amp;可观察的:

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