Cla*_*aus 1 javascript firebase vue.js vuefire google-cloud-firestore
我目前正在使用带有 Vuefire 支持的 Firestore/Vue.js。
Firestore DB 只有一个集合,其中有几个用户:
- 用户
- 001
- 姓名:杰克
- uid:{Firebase 身份验证 ID}
- 组织 ID:123
- 002
- 姓名:弗兰克
- uid {Firebase 身份验证 ID}
- 组织 ID:456
在 Vue 组件中,我尝试查询数据库以获取使用身份验证 ID 的第一个用户(当前存储在 Vuex 存储中)
<script>
import { db } from "../main.js";
export default {
data() {
return {
items: [] // to be used later
};
},
created() {
this.getServices();
},
methods: {
getServices() {
console.log(this.$store.state.user.uid);
db.collection("users")
//.doc("001")
.where("uid", "==", this.$store.state.user.uid)
.get()
.then((snapshot) => {
console.log(snapshot);
if (snapshot != null && snapshot.data != null) {
const user = snapshot.data();
// do something with document
let org_id = user["org_id"];
console.log("org id:" + org_id);
} else {
console.log("No data returned!");
}
});
},
},
};
</script>
Run Code Online (Sandbox Code Playgroud)
该代码始终返回一个空快照。我已执行的检查:
this.$store.state.user.uid已正确设置uid会产生相同的错误我是一个完全的初学者,但在我看来,where 子句不起作用。
因为,当db.collection("users").where("uid", "==", this.$store.state.user.uid)您定义 a时Query,该snapshot对象实际上是 aQuerySnapshot而不是 a DocumentSnapshot。
所以snapshot.data != null总是false因为 aQuerySnapshot不具有这样的属性。=>也是如此,snapshot.data() != null总是false因为 aQuerySnapshot没有这样的方法。
您应该循环使用QuerySnapshot方法forEach()或使用map属性,如Vuefire 示例docs所示(请参阅“检索集合”):
db.collection("users")
.where("uid", "==", this.$store.state.user.uid)
.get()
.then((snapshot) => {
const documents = snapshot.docs.map(doc => doc.data())
// do something with documents
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1000 次 |
| 最近记录: |