[英]Angular and firebase - Cannot Map of undefined on first load
It works after the first load, I need to know how to provide a promise to prevent the data mapping without first letting it load. 它在第一次加载后就可以工作,我需要知道如何在不先加载的情况下提供防止数据映射的承诺。 On first load of the site it displays the error below. 首次加载该网站时,它会在下面显示错误。 I think it's cause by not allowing the colleciton of data from the database before trying to map it? 我认为是由于在尝试映射之前不允许来自数据库的数据集合引起的?
Console error 控制台错误
DashboardComponent_Host.html:1 ERROR TypeError: Cannot read property 'map' of undefined at DashboardComponent.webpackJsonp.../../../../../src/app/dashboard/dashboard.component.ts.DashboardComponent.populateDashboard (dashboard.component.ts:70) at DashboardComponent_Host.html:1错误TypeError:无法读取DashboardComponent.webpackJsonp ... / .. / .. / .. / .. / src / app / dashboard / dashboard.component.ts.DashboardComponent上未定义的属性“ map”。 populateDashboard(dashboard.component.ts:70)位于
For context the line 70 is this.goalsLength eg the first line that calls the db. 对于上下文,第70行是this.goalsLength,例如,调用db的第一行。
The TS file TS文件
ngOnInit() {
this.populateDashboard();
}
populateDashboard() {
// just needs a filter on active = if active its not completed. CHANGE IN DB FROM ACTIVE TO COMPLETED
this.goalsLength = this.progressService.getActiveGoals().map(goals => {
return goals.length;
});
this.visionsLength = this.progressService.getCompletedVisions().map(visions => {
return visions.length;
});
this.opportunitiesLength = this.progressService.getCompletedOpportunities().map(opportunities => {
return opportunities.length;
});
this.actionPlansLength = this.progressService.getCompletedActionPlans().map(actionPlans => {
return actionPlans.length;
});
Service 服务
userId: string;
completedVisions: FirebaseListObservable<VisionItem[]> = null;
activeGoals: FirebaseListObservable<Goal[]> = null;
opportunities: FirebaseListObservable<Goal[]> = null;
actionPlans: FirebaseListObservable<Goal[]> = null;
constructor(private db: AngularFireDatabase,
private afAuth: AngularFireAuth) {
// setting userId returned from auth state to the userId on the service, now we can query
// currently logged in user, using the id. IMPORTANT
this.afAuth.authState.subscribe(user => {
if (user) {
this.userId = user.uid
}
});
}
// Used to get the dashboard values.
getCompletedVisions(): FirebaseListObservable<VisionItem[]> {
if (!this.userId) { return; } // if undefined return null.
this.completedVisions = this.db.list(`visions/${this.userId}`, {
query: {
orderByChild: 'completed',
equalTo: true
}
});
return this.completedVisions;
}
getCompletedOpportunities(): FirebaseListObservable<Goal[]> {
if (!this.userId) { return }; // if undefined return null.
this.opportunities = this.db.list(`goals/${this.userId}`, {
query: {
orderByChild: 'opportunitiesCompleted',
equalTo: true
}
});
return this.opportunities;
}
getCompletedActionPlans(): FirebaseListObservable<Goal[]> {
if (!this.userId) { return }; // if undefined return null.
this.actionPlans = this.db.list(`goals/${this.userId}`, {
query: {
orderByChild: 'allActionPlanFieldsCompleted',
equalTo: true
}
});
return this.actionPlans;
}
// Dashboard related queries.
getActiveGoals(): FirebaseListObservable<Goal[]> {
if (!this.userId) { return }; // if undefined return null.
this.activeGoals = this.db.list(`goals/${this.userId}`, {
query: {
orderByChild: 'completed',
equalTo: true
}
});
return this.activeGoals;
}
Just as the error messages state, you are willing to return FirebaseListObservable
from all service functions and subscribing them from component, but service functions will return null
when this.userId
doesn't exist or unsetted . 就像错误消息指出的那样,您愿意从所有服务函数返回FirebaseListObservable
并从组件中订购它们,但是当this.userId
不存在或未设置时 ,服务函数将返回null
。
The reason this happened is because you are setting this.userId
in an asynchronous way( this.afAuth.authState.subscribe
). 发生这种情况的原因是因为您正在以异步方式设置this.userId
( this.afAuth.authState.subscribe
)。
Just be sure all branches return Observable
, for example: 只要确保所有分支都返回Observable
,例如:
if (!this.userId) { return Observable.of([]); } // provide an empty array if userId is not yet ready
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.