在一个离子项目中,我有一个页面'user Profile' . 我想加载相对于已登录用户的数据详细信息(经过身份验证) . 这些详细信息位于Firestore Collection(用户)中,我在集合中有一个字段userGuid . 此字段与经过身份验证的用户的uid相同 . 此系列还有其他领域,如年龄,名称等 . 因为我不想加载所有用户集合,我正在使用此代码,它运行良好...但我必须先指定docID,它不是目标:

...

import {AngularFirestore} from 'angularfire2/firestore';
...
    Export class MyprofilePage {


      docID = 'yJQgd7xSBFBl3BYAWaiy';

      userRef = this.afs.collection('Users').doc(this.docID).ref.get().then(function(doc) {
      if (doc.exists) {
        console.log("Document data:", doc.data());
      } else {
        console.log("No such document!");
      }
      });


      constructor(public navCtrl: NavController, public navParams: NavParams, private authService: AuthService, private afs: AngularFirestore) {
        this.userID = this.authService.getActiveUser().uid;
        console.log("ActiveUser: "+this.userID);
      }

我想动态分配(或替换)docID与当前用户的id(我在构造函数中得到) userRef = this.afs.collection('Users').doc(this.userID).ref.get().then(function(doc)....

但这不起作用,代码返回没有这样的文件 . 我试图将userRef代码放在构造函数或Nginit中,但需要声明userRef . 它可能很简单,但我无法实现这一点 . 我理解我可以定义一个类似“loadProfile()”的方法,并在HtMl上使用un按钮(单击)事件调用它,但我希望在用户访问页面时为用户加载数据 .

PS:身份验证和userID部分工作得很好!我在进入此页面时成功地在控制台中看到了userID .