【发布时间】:2019-01-11 17:06:30
【问题描述】:
在我的 Angular 应用中,我使用的是 Angularfire2 和 Firestore。
从文档中我知道我可以使用以下命令(其中 db 是 AngularFirestore 的实例)通过电子邮件“xyz@gmail.com”获取用户:
this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"));
然而,
let user = this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"));
不会在变量“user”中保存带有电子邮件“xyz@gmail.com”的用户,而是在整个集合“users”中保存。
如何通过电子邮件“xyz@gmail.com”获取用户? 然后如何获取用户的属性(例如,用户的 id)?
更新:
我尝试了以下方法:
给出的错误消息是属性“then”在类型 AngularFirestoreCollection 上不存在。
我还尝试了以下方法:
let user = this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"))
.valueChanges()
.pipe(reduce((acc, curr) => curr[0], {}));
这也给出了语法错误。
最后,我只是尝试了这个:
let users = this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"));
let user = users[0];
console.log(user);
这并没有给出语法错误。但是,“用户”是未定义的。
更新 2:
我也试过这个:
private async getUser() {
let users = await this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"));
let user = users[0];
console.log(user);
}
但是,“用户”仍然是“未定义的”。
P.S.:电子邮件“xyz@gmail.com”的用户确实存在于 firestore。
更新 3:
我还尝试了以下方法:
但是,与更新 1 中使用的版本一样,我收到“减少”错误 ...错误消息说:
Cannot find name "reduce"
【问题讨论】:
标签: angular firebase google-cloud-firestore angularfire