【问题标题】:how can i match with current user Id with firebase database data's value?如何将当前用户 ID 与 firebase 数据库数据的值相匹配?
【发布时间】: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


【解决方案1】:

您必须先检查db.collection('cartitems').where('user', '==', this.user.alias).get()。是对还是错?然后我认为它不像where('user', '==', this.user.alias)那样直接工作你应该得到第一个密钥,你可以在firebase中比较它,我希望你能理解。

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 2011-04-04
    • 2015-03-12
    • 2021-03-03
    • 2020-07-11
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多