首页 > 解决方案 > 来自firebase的Vuex值分配

问题描述

我使用此代码将数据从 firebase 实时数据库下载到我的 vuex 商店。

 firebase.database().ref('Profiles').child(firebase.auth().currentUser.uid).once('value')
  .then((data) => {
    var obj = data.val()
    console.log("DATA.VAL().votedQuests", data.val().votedQuests)
    const profile = {
      id: obj.id,
      votedQuests: data.val().votedQuests,
    }

结果在 data.val().votedQuests 我得到:

console print:
    DATA.VAL().votedQuests: -M3pc4Bf4bSjqeSw1CvB: "Tak"

什么是正确的:

screen:来自firebase的数据结构

但是当我这样做时:

const profile = {
      id: obj.id,
      votedQuests: data.val().votedQuests,
    }

或者

const profile = {
      id: obj.id,
      votedQuests: obj.votedQuests,
    }

并尝试在 console.log("Profile",profile) 中打印结果,我得到了这个视图:

console print:
votedQuests: Object
         -M3pc4Bf4bSjqeSw1CvB: "Tak"
         rbFeBq6lckgH7k1KZr419Gz9cGJ3: undefined //this key rbFeB... is uid of user. It should not be here.

你能向我解释为什么这个键rbFeBq6lckgH7k1KZr419Gz9cGJ3: undefined在我的 votedQuests 下,而在 firebase 中它不存在吗?

当我 在正确obj.votedQuests之前检查值时,更重要的是。const profile{...}

标签: javascriptfirebasevue.jsfirebase-realtime-databasevuex

解决方案


推荐阅读