Angular2 / Firebase - EXCEPTION:TypeError:无法读取属性' push'未定义的

时间:2016-12-06 15:02:58

标签: angular typescript firebase firebase-realtime-database push

我正在尝试创建Angular2和Firebase Web应用程序(CRUD)。我尝试添加新的业务用户时出错。

appComponent.ts文件:

    import { Component, OnInit} from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';
import {FirebaseService} from './services/firebase.service';
import 'rxjs/add/operator/map';
@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  providers:[FirebaseService]
})

export class AppComponent implements OnInit {
   title = 'app works';
   BusinessUsers : Business[];
   categories : Category[];
   appState: string;
   activeKey: string;

   constructor(private _firebaseService:FirebaseService) {

   }
   ngOnInit(){
     this.appState = 'default';
     this._firebaseService.getBusinessUsers().subscribe(BusinessUsers => {
       this.BusinessUsers = BusinessUsers
     });

     this._firebaseService.getCategories().subscribe(categories => {
       this.categories = categories
     });

   }

   changeState(state, key = null){
    if(key){
      this.activeKey = key;
    }
    this.appState = state;
  }




  addBusiness(
    website: URL,
  ){
    var created_at = new Date().toString();

    var newBusiness = {

     website: website,
     created_at:created_at
  }
    this._firebaseService.addBusiness(newBusiness);

    this.changeState('default');
  }
}

export interface Business {
    $Key:string;
    address: string;
    company?: string;
    zipcode: number;
    suite: Date;
    email: string;
    name: string;
    website: URL;

}

export interface Category {
    $Key:string;
    value: string;
}

firebase.service.ts档案中:

    import {Injectable} from '@angular/core';
import { AngularFire, FirebaseListObservable } from 'angularfire2';


@Injectable()
export class FirebaseService{
    BusinessUsers : FirebaseListObservable<Business[]>;
    categories : FirebaseListObservable<Category[]>;

    constructor(private af:AngularFire) {

    }

    getBusinessUsers(){
        this.BusinessUsers = this.af.database.list('/BusinessUsers') as FirebaseListObservable<Business[]>;
        return this.BusinessUsers;
    }
    getCategories(){
        this.categories = this.af.database.list('/Categories') as FirebaseListObservable<Category[]>;
        return this.categories;
    }

    addBusiness(newBusiness): Promise<any>{
      return this.businessUsers.push(newBusiness);
    }
}


export interface Business {
    $Key:string;
    address: string;
    company?: string;
    zipcode: number;
    suite: Date;
    email: string;
    name: string;
    website: URL;

}

export interface Category {
    $Key:string;
    value: string;
}

这是我写文本并单击添加时的错误图像:

enter image description here

我有一个Firebase数据库。

1 个答案:

答案 0 :(得分:0)

这是命名惯例问题。取代

BusinessUsers : FirebaseListObservable<Business[]>;

businessUsers : FirebaseListObservable<Business[]>;

在您的服务文件中。这里 BusinessUsers 应该是 businessUsers 。希望这会奏效。