错误错误:未捕获(在承诺中):TypeError:this.db.list(...)。map不是函数TypeError:this.db.list(...)。map不是函数

时间:2017-06-01 07:28:47

标签: angular firebase observable

  

错误错误错误:未捕获(在承诺中):TypeError:this.db.list(...)。map不是函数TypeError:this.db.list(...)。

地图不是一个函数可能是映射中的错误我不知道为什么火灾基础列表可以观察到没有正确映射的

import {Observable} from "rxjs/Rx";

export class employee {

  constructor(
  public $key:string,
  public age:number,
  public username: string,
  public email:string,
  public yoe:number
  )

  {}

    //   static fromJsonList(array): employee[] {
    //         return array.map(employee.fromJson);
    //     }

    // static fromJson({$key,age,username,email,yoe}):employee {
    //     return new employee(
    //         $key,age,username,email,yoe);
    // }
    static fromJson({$key, age,username,email,yoe}) {
        return new employee($key, age,username,email,yoe);
    }
        static fromJsonArray(json : any[]) : employee[] {
        return json.map(employee.fromJson);
    }

}

我的下一个文件

import { Injectable } from '@angular/core';
import { AngularFireDatabase, FirebaseListObservable,FirebaseObjectObservable } from 'angularfire2/database';
import { AngularFireAuth} from 'angularfire2/auth';

import {Observable} from 'rxjs/Observable';

import{employee}from'./emp'

@Injectable()
export class SearchService {
  constructor(private db:AngularFireDatabase) 
  { }

  findAllemployees():Observable<employee[]> {
    return this.db.list('/user').map(employee.fromJsonArray);
  }

}

错误

import {Router} from '@angular/router';
import {FirebaseListObservable, FirebaseObjectObservable,AngularFireDatabase} from 'angularfire2/database';
import { Component, OnInit ,Pipe,PipeTransform} from '@angular/core';

import{employee}from'../emp'

import { AngularFireAuth} from 'angularfire2/auth';
import {Observable} from 'rxjs/Observable';
import { Subject }           from 'rxjs/Subject';
import{SearchService} from '../search.service';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';


@Component({
  selector: 'app-searchuser',
  templateUrl: './searchuser.component.html',
  styleUrls: ['./searchuser.component.css'],
  providers:[SearchService]

})
export class SearchuserComponent implements OnInit {

  b:employee[];
  heroes: Observable<employee[]>;
  item:FirebaseListObservable<any[]>;
  i:FirebaseObjectObservable<any>;
  constructor( private db:AngularFireDatabase,
    private router: Router,private ser:SearchService) 
  {
    console.log(this.item);
  }

  ngOnInit() {
    this.heroes=this.ser.findAllemployees();
  }
}

1 个答案:

答案 0 :(得分:0)

您需要使用

之类的内容配置Firebase

<强> firebase.config.ts

import {AuthMethods, AuthProviders} from 'angularfire2';

export const firebaseConfig = {
    apiKey: 'blablabla ---- bla',
    authDomain: 'myproject.firebaseapp.com',
    databaseURL: 'https://myproject.firebaseio.com',
    storageBucket: 'gs://myproject.appspot.com/',
    messagingSenderId: '123456789'
};



export const authConfig = {
    provider: AuthProviders.Password,
    method: AuthMethods.Password
};

通过Firebase Web控制台(https://console.firebase.google.com)初始化项目时,将为您提供Firebase配置中使用的实际参数。

然后,在 app.module.ts 文件中,插入

  imports: [
    .....
    AngularFireModule.initializeApp(firebaseConfig, authConfig),
    ......
  ],

然后在使用AngularFire的服务类中,我使用以下代码(这是使用AngularFire版本angularfire2@2.0.0-beta.8)

.....
import { AngularFire, FirebaseListObservable } from 'angularfire2';
.....
@Injectable()
export class MyService {

  constructor(private af: AngularFire) { }

  loadStuff() {
    return this.af.database.list('/STUFF').map(stuff.fromJsonArray);
  }

...
}