简体   繁体   English

Angular和Firebase-首次加载时无法映射未定义

[英]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.userIdthis.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.

相关问题 第一次页面加载角度未定义 - on first time page load Angular is undefined 可以在第一次渲染时 map 但在刷新时显示不能 map 未定义 - Can map on first render but on refresh shows cannot map of undefined 角谷歌地图在组件加载时变得不确定 - Angular google map getting undefined lat long on components load 发布到firebase时,Angular无法读取未定义的属性推送 - Angular cannot read property push of undefined when posting to firebase Angular 2在初始化第二个映射时无法读取未定义的属性“maps” - Angular 2 Cannot read property 'maps' of undefined while init second map 错误TypeError:无法读取未定义角度4的属性“地图” - ERROR TypeError: Cannot read property 'map' of undefined angular 4 无法读取未定义的地图 - cannot read map of undefined 错误:“无法实例化 firebase-storage - 请务必先加载 firebase-app.js” - Error: "Cannot instantiate firebase-storage - be sure to load firebase-app.js first" Angular2,Typescript:数组map()方法:推送嵌套元素。 无法读取未定义的属性“地图” - Angular2,Typescript: array map() method: Pushing nested elements. Cannot read property 'map' of undefined 未处理的拒绝(类型错误):无法读取未定义的属性“地图”? (反应火力基地) - Unhandled Rejection (TypeError): Cannot read property 'map' of undefined? (React-firebase)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM