【问题标题】:Angular & Firebase - checking a node before subscribing to an activated routeAngular & Firebase - 在订阅激活的路由之前检查节点
【发布时间】:2017-08-10 22:43:23
【问题描述】:

我正在使用什么

  • 角度
  • Firebase

我要做什么

  • 检查 firebase 节点以查看用户 ID 是否为真/是否存在

  • 如果是,则显示来自另一个节点的项目列表

问题

  • 我有这两个功能独立工作
  • 我可以检查用户节点以查看它们是否存在并写入控制台
  • 我可以显示项目列表
  • 但是我不能检查用户然后显示项目列表

这是检查“成员”节点的代码

这是我用来查看用户密钥是否存在于成员>项目ID下的

    var userId = firebase.auth().currentUser.uid;
    var pathToCheck = '/members/' + this.projectId + '/' + userId;

    firebase.database().ref(pathToCheck).once('value').then(function (snapshot) {
      if (snapshot.exists()) {
        console.log('hooplar!');
      } else {
        console.log('hmmm');
      }
    });
  }

这就是我显示项目的方式

    this.activatedRoute.data.subscribe((
      data: { issueListData: any }) => {
      this.issuesList = data.issueListData;
    });  

我希望发生的事情

理想情况下,这是我想要的功能,但会引发以下错误:

错误错误:未捕获(在承诺中):TypeError:无法读取未定义的属性“activatedRoute”

    var userId = firebase.auth().currentUser.uid;
    var pathToCheck = '/members/' + this.projectId + '/' + userId;

    firebase.database().ref(pathToCheck).once('value').then(function (snapshot) {
      if (snapshot.exists()) {
        this.activatedRoute.data.subscribe((
          data: { issueListData: any }) => {
          this.issuesList = data.issueListData;
        });
        console.log('hooplar!');
      } else {
        console.log('hmmm');
      }
    });
  }

【问题讨论】:

标签: angular firebase firebase-realtime-database firebase-authentication


【解决方案1】:

您没有绑定 this 正确更改您的 then 到箭头函数以访问外部范围 this。或者在你的 then 函数的末尾通过右括号添加 }.bind(this) 我假设 this.activatedRoute 是你的类道具对吗?你 then 函数中的 this 指的是数据库承诺而不是你的类

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 2016-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-26
    相关资源
    最近更新 更多