错误:未捕获(承诺):TypeError:未定义不是函数

时间:2019-07-03 18:41:34

标签: angular firebase

我正在尝试在html组件上显示汽车列表,但这会引发错误...我正在使用Firebase

服务

import { Injectable } from '@angular/core';

import { AngularFirestore, AngularFirestoreCollection, 
AngularFirestoreDocument } from '@angular/fire/firestore';

import { Cars } from '../models/cars';
import { AuthService } from './auth.service';
import { ToastrService } from './toastr.service';
import { User } from '../../shared/models/user';

@Injectable()
export class BusesService {

userDetail: User;

buses: AngularFirestoreCollection<Cars>;
buse : AngularFirestoreDocument<Cars>;

constructor(
  private db: AngularFirestore,
  private authService: AuthService,
  private toastrService: ToastrService
  ) {
 }
 getBuses(){ 
   this.userDetail = this.authService.getLoggedInUser();

   this.buses = this.db.collection('cars',
   ref => ref.where('userId', '==', this.userDetail.$key));

   return this.buses;
  }
 }

我在其中使用服务的组件

import { Component, OnInit } from '@angular/core';
import { Cars } from '../../../shared/models/cars';
import { AuthService } from '../../../shared/services/auth.service';
import { BusesService } from '../../../shared/services/buses.service';
import { ToastrService } from 'src/app/shared/services/toastr.service';

@Component({
 selector: 'app-lista-camiones',
 templateUrl: './lista-camiones.component.html',
 styleUrls: ['./lista-camiones.component.scss']
 })

export class ListaCamionesComponent implements OnInit {

 productList: Cars[];
 productObject: Cars;

 loading = false;

constructor(
  public authService : AuthService,
  private busesService : BusesService,
  private toastrService : ToastrService
) { }

ngOnInit() {
  this.getAllProducts();
}

  getAllProducts(){
    console.log("getting Cars");

   this.loading = true;
    const x = this.busesService.getBuses();
    x.snapshotChanges().subscribe(
      (product) => {
        this.loading = false;

        this.productList = [];
        console.log(" products" + product);

        product.forEach((element) => {

          this.productObject = element.payload.doc.data();
          this.productObject.userid = element.payload.doc.id;
          console.log("data : " + this.productObject.userid);
          this.productList.push(this.productObject as Cars);
        });
      },
      (err) => {
        this.toastrService.error('Error while fetching Car List', err);
      }
     );

    }
}

模型

export class Cars {
   userid: string;
   driverid: number;
   unitid: string;
   description: string;
}

错误

  

ERROR错误:未捕获(承诺):TypeError:未定义不是   函数TypeError:undefined不是函数       在Array.map()       在webpackAsyncContext($ _lazy_route_resource惰性名称空间对象:45)

0 个答案:

没有答案