2017-09-01 135 views
0

我試圖從Firebase中檢索單個對象,但它沒有返回任何值。我有這個消息,而不是「FirebaseObjectObservable {_isScalar:false,$ ref:Reference,source:FirebaseObjectObservable,operator:ObserveOnOperator} 「FirebaseObjectObservable返回null值

這就是我如何編寫我的服務,getDataList()方法完美工作。get getDataD()應該檢索單個對象是我關心的問題。

import { Injectable } from '@angular/core'; 
import { AngularFireDatabase, FirebaseListObservable , FirebaseObjectObservable} from 'angularfire2/database'; 
import { Data } from '../data'; 
import { AngularFireModule } from 'angularfire2'; 


@Injectable() 
export class Ng2fireServiceService { 

    private basePath: string = '/data'; 
    dataL: FirebaseListObservable<Data[]> = null; // list of objects 
    dataD: FirebaseObjectObservable<Data> = null; // single object 

    constructor(private af: AngularFireModule, 
       private db: AngularFireDatabase) { } 


    getDataList(query={}): FirebaseListObservable<Data[]> { 
    this.dataL = this.db.list(this.basePath, { 
    query: query 
    }); 
    return this.dataL 
    } 
// Return a single observable item 
    getDataD(key: string): FirebaseObjectObservable<Data> { 
    const dataPath = `${this.basePath}/${key}`; 
    this.dataD = this.db.object(dataPath) 
    return this.dataD 
    } 

} 

,這我怎麼稱呼它在我的組件

import { Ng2fireServiceService } from '../../services/ng2fire-service.service'; 
import { Component, OnInit} from '@angular/core'; 
import { Data } from '../../data'; 
import { Router, ActivatedRoute, Params } from '@angular/router'; 



@Component({ 
    selector: 'app-data-detail', 
    templateUrl: './data-detail.component.html', 
    styleUrls: ['./data-detail.component.css'] 
}) 
export class DataDetailComponent implements OnInit { 

    id : string ; 
    dataD : any; 

    constructor(public authService :AuthService, private SVC : Ng2fireServiceService,private router : Router, 
    private route : ActivatedRoute) { } 

    ngOnInit() { 

    this.id = this.route.snapshot.params['id']; 

    this.dataD = this.SVC.getDataD(this.id); 

    console.log(this.dataD); 
    } 

}

你對如何處理這個

回答

1

因爲你不訂閱可觀察到的任何IDEO。試試這個:

ngOnInit() { 

    this.id = this.route.snapshot.params['id']; 

    this.SVC.getDataD(this.id).subscribe(val => this.dataD = val);