【发布时间】:2018-12-03 05:31:34
【问题描述】:
我将 vue.js 与 firebase 一起用于购物网站。 我可以像下面这样获取当前用户 ID。
data () {
return {
cartitems: [],
user: null,
}
},
created(){
//get current user
db.collection('users').where('user_id','==',firebase.auth().currentUser.uid).get()
.then(snapshot=>{
snapshot.forEach(doc => {
this.user =doc.data(),
this.user.id = doc.id
})
console.log('get current user id')
console.log(this.user.id)
console.log(this.user.alias)
})
它正在工作,我可以获得当前的用户 ID 和别名。
在“cartitems”中,我将 user.alias 存储在“用户”中。 我只想在“当前用户别名 = cartiems.user”中显示 caritem。 但下面的代码不起作用。 它成功获取当前用户别名,我在控制台中签入。
db.collection('cartitems').where('user', '==', this.user.alias).get()
.then(snapshot => {
snapshot.forEach(doc => {
this.cartitem = doc.data()
this.cartitem.id = doc.id
this.cartitems.push(cartitem)
})
}).catch(function(error) {
console.log("Error getting documents: ", error);
})
无论当前用户信息如何,当我获取 caritems 中的所有 caritem 时, 它正在工作。代码如下。
db.collection('cartitems').get()
.then(snapshot =>{
snapshot.forEach(doc =>{
let cartitem = doc.data()
cartitem.id = doc.id
this.cartitems.push(cartitem)
})
})
我只想显示与当前用户匹配的 caritem。请帮忙:) 谢谢。
【问题讨论】:
-
请为您尝试查询的文档添加数据库控制台的屏幕截图。另外,当你在查询之前记录
this.user.alias时,它记录了什么值?
标签: javascript firebase vue.js google-cloud-firestore