我正在尝试使用 Angular 从 Firestore 文档中获取各个值。
我的 TypeScript 代码:
userDoc: AngularFirestoreDocument<User>;
user: Observable<User>;
ngOnInit() {
this.userDoc = this.afs.doc('users/' + this.af.auth.currentUser.uid);
this.user = this.userDoc.valueChanges();
}
然后是 HTML:
{{ user | async | json }}
返回此:
{ "email": "[email protected]", "firstname": "x", "lastname": "x", "phone": "x", "subscription": x, "uid": "x" }
这正是我需要的......
但是当我尝试获得像这样的个人价值时:
<p>
Your email is: {{ user.email | async }}
</p>
我无法获取单个值。如何“解开”可观察值以获得单个值?
我找到了答案,很简单:
{{(user | async)?.email }}
解开数据。如果有人能更好地解释这一点那就太好了!
this.firestoreData = this.firestore.getCollection(this.dataPath); //where dataPath = the collection name
this.firestoreData.subscribe(firestoreData => {
for (let dID of firestoreData ) {
if (dID.field1 == this.ClassVariable1) {
if (dID.field2 == '') {
this.ClassVariable2 = dID.id; //assign the Firestore documentID to a class variable for use later
}
}
}
} );