我编写了一个代码,该代码显示新闻列表,并将其存储在firestore中,并且一切正常,但是当我单击“详细信息”按钮以显示新闻的详细信息时,它在控制台上显示错误,但未显示页面上的详细信息 错误消息: DetailsPage_Host.ngfactory.js? [sm]:1错误TypeError:无法读取未定义的属性“ doc” 在DetailsPage.push ../ src / app / details / details.page.ts.DetailsPage.getEventDetail(details.page.ts:57)处 在DetailsPage.push ../ src / app / details / details.page.ts.DetailsPage.ngOnInit(details.page.ts:33)
我的代码: news.page.html
<ion-content padding>
<ion-item *ngFor=" let count of news | async">
<h5>{{count?.title}}
<button routerLink="/details/{{count.id}} ">details</button>
<img src="{{count?.image}}">
</h5>
</ion-item>
</ion-content>
news.page.ts
import { Component, OnInit } from '@angular/core';
import {AngularFirestore, AngularFirestoreDocument} from 'angularfire2/firestore';
import {Observable} from 'rxjs';
import { Router } from '@angular/router';
import 'rxjs/add/operator/map';
export class NewsPage implements OnInit {
news: Observable<any[]>;
constructor(public db: AngularFirestore, private router: Router) { }
ngOnInit() {
this.news = this.db.collection('123').snapshotChanges().map(actions => {
return actions.map(a => {
const data = a.payload.doc.data();
const id = a.payload.doc.id;
return { id, ...data };
});
});
}
}
details.page.html
<ion-content padding >
${{currentEvent?.content}}
</ion-content>
details.page.ts
import { Component, OnInit, ɵConsole } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AngularFirestore, AngularFirestoreDocument } from 'angularfire2/firestore';
import {Observable} from 'rxjs';
export class DetailsPage implements OnInit {
public eventListRef: firebase.firestore.CollectionReference;
public currentEvent: any = {};
constructor(private router: ActivatedRoute, private afs: AngularFirestore) {
}
ngOnInit() {
const newsId: string = this.router.snapshot.paramMap.get('id');
console.log(newsId);
this.getEventDetail(newsId).get().then(eventSnapshot => { //the error message 33 is Here
this.currentEvent = eventSnapshot.data();
this.currentEvent.id = eventSnapshot.id;
});
}
getEventDetail(newsId: string): firebase.firestore.DocumentReference {
return this.eventListRef.doc(newsId);**//the error message 57 is Here**
}
}
我该如何解决这个问题?