我正在尝试根据用户的用户 ID 的单个查询搜索来获取要添加到状态的用户数据。我尝试执行单个文档路径,但这对我不起作用,因为 firebase.auth().currentUser.uid 与 doc.id 不同,我现在使用的代码是:
firebase.firestore()
.collection("users").get().then((querySnapshot) => {
querySnapshot.forEach((doc) => {
console.log(firebase.auth().currentUser.email);
console.log(doc.data().email;
if (doc.data().email) == firebase.auth().currentUser.email) {
this.setState({
avatar: doc.data().avatar,
name: doc.data().name,
email: doc.data().email
});
}
});
});
虽然此代码有效,但效率非常低,因为它会遍历所有用户并将他们的电子邮件与当前用户的电子邮件进行比较。我想知道是否有一种方法可以不使用这种方法而只搜索数据。任何帮助将不胜感激。预先感谢您!
最佳答案
尝试做这样的事情,如果对你有用,请发表评论:
firebase.firestore()
.collection("users")
.where('email', '==', firebase.auth().currentUser.email)
.get()
.then((querySnapshot) =>
querySnapshot.forEach((doc) =>
this.setState({
avatar: doc.data().avatar,
name: doc.data().name,
email: doc.data().email
})
)
);
关于javascript - 如何在 native react 中使单个文档的 Firebase 查询更有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59941550/