如何根据 属性 从 firestore 检索用户
How to retrieve user from firestore based on property
在我的 angular 应用中,我使用的是 Angularfire2 和 Firestore。
从文档中我知道我可以使用以下命令通过电子邮件 "xyz@gmail.com" 获取用户(其中 db 是 AngularFirestore 的一个实例):
this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"));
然而,
let user = this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"));
不会将用户的电子邮件 "xyz@gmail.com" 保存在变量 "user" 中,而是将整个集合 "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);
这并没有给出语法错误。但是,"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);
}
然而,"user" 仍然是 "undefined"。
P.S.: 具有电子邮件 "xyz@gmail.com" 的用户确实存在于 firestore 中。
更新 3:
我还尝试了以下方法:
但是,与更新 1 中使用的版本一样,我收到 "reduce" 的错误
...错误消息说:
Cannot find name "reduce"
首先是存储新用户。推荐的方法是 call .doc()
or .add()
以便 Firebase SDK 自动生成一个唯一的 ID 字符串。这是一种方法尺度(users/some-id-ha87sdhf87hjakshdf7323r/"displayName":"john smith" vs 存储为 users/johnsmith
)。
因此,假设您的 users
集合中充满了个人用户,每个用户都有一个 Firestore 生成的 uid,只需查询您想要的内容即可。
Firestore 的优点在于它支持复合查询。参见:Perform simple and compound queries in Cloud Firestore
let user = this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"));
不会为您提供一个用户,而是为您提供一组具有该电子邮件地址的用户 属性(数据库中可以有多个用户)
如果你知道你只有一个,我想你可以使用:
.valueChanges()
.pipe(reduce((acc, curr) => curr[0], {}))
仅获取集合中的第一个元素
好的,我找到了问题的解决方案:
this.userData = this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com").valueChanges();
this.userData.subscribe(users => {
this.user = users[0];
console.log("this is the user:" + this.user);
});
在我的 angular 应用中,我使用的是 Angularfire2 和 Firestore。
从文档中我知道我可以使用以下命令通过电子邮件 "xyz@gmail.com" 获取用户(其中 db 是 AngularFirestore 的一个实例):
this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"));
然而,
let user = this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"));
不会将用户的电子邮件 "xyz@gmail.com" 保存在变量 "user" 中,而是将整个集合 "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);
这并没有给出语法错误。但是,"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);
}
然而,"user" 仍然是 "undefined"。
P.S.: 具有电子邮件 "xyz@gmail.com" 的用户确实存在于 firestore 中。
更新 3:
我还尝试了以下方法:
但是,与更新 1 中使用的版本一样,我收到 "reduce" 的错误 ...错误消息说:
Cannot find name "reduce"
首先是存储新用户。推荐的方法是 call .doc()
or .add()
以便 Firebase SDK 自动生成一个唯一的 ID 字符串。这是一种方法尺度(users/some-id-ha87sdhf87hjakshdf7323r/"displayName":"john smith" vs 存储为 users/johnsmith
)。
因此,假设您的 users
集合中充满了个人用户,每个用户都有一个 Firestore 生成的 uid,只需查询您想要的内容即可。
Firestore 的优点在于它支持复合查询。参见:Perform simple and compound queries in Cloud Firestore
let user = this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com"));
不会为您提供一个用户,而是为您提供一组具有该电子邮件地址的用户 属性(数据库中可以有多个用户) 如果你知道你只有一个,我想你可以使用:
.valueChanges()
.pipe(reduce((acc, curr) => curr[0], {}))
仅获取集合中的第一个元素
好的,我找到了问题的解决方案:
this.userData = this.db.collection("users", ref => ref.where('email', '==', "xyz@gmail.com").valueChanges();
this.userData.subscribe(users => {
this.user = users[0];
console.log("this is the user:" + this.user);
});