Vue.js。 Firebase查询-如何从查询中获得成功?

时间:2018-06-27 13:40:28

标签: firebase firebase-realtime-database vue.js vuex

我有一个只有一个测试值的Firebase实时数据库

{
"details": {
     "uid": "mayU7Zyc3GMC4STXB6mVu32xFO13",
     "honorificPrefix": "Mr.",
     "givenName": "Yves",
     "familyName": "Dufour",
     "address1": "3, chemin des buis",
     "address2": "Les Ballans",
     "zipcode": "16600",
     "city": "Mornac",
     "country": "France"
 }
}

我正在尝试从vue组件获取此测试值

 getUserDetail ({commit}, payload) {
    console.log('ACTION: getUserDetail db: ', payload.db)
    console.log('ACTION: getUserDetail id: ', payload.uid)
    payload.db.ref('details').orderByChild('uid').equalTo('uid', payload.uid).once('value', (snapshot) => {
    // db.ref('details').orderByChild('userId').once('value', (snapshot) => {
      console.log('SNAPSHOT VAL ', snapshot.val())
    })
  }

运行它,我可以访问详细信息,但实际上并没有得到预期的结果

console.log('SNAPSHOT VAL ', snapshot.val()) // is null

这是Firebase的控制台日志输出

ACTION: getUserDetail db:  Database {repo_: Repo, root_: Reference, INTERNAL: DatabaseInternals}INTERNAL: DatabaseInternals {database: Database}app: (...)repo_: Repo {repoInfo_: RepoInfo, app: FirebaseAppImpl, dataUpdateCount: 1, statsListener_: null, eventQueue_: EventQueue, …}root_: Reference {repo: Repo, path: Path, queryParams_: QueryParams, orderByCalled_: false}__proto__: Object
root.js?f07d:131 


ACTION: getUserDetail id:  mayU7Zyc3GMC4STXB6mVu32xFO1


[FIREBASE]  getToken() completed. Creating connection. 
main.js?1c90:62 [FIREBASE]  c:0:0: Connection created 
main.js?1c90:62 [FIREBASE]  c:0:0:0 Websocket connecting to xxxxxxxxxxx
main.js?1c90:62 [FIREBASE]  c:0:0:0 Websocket connected. 
main.js?1c90:62 [FIREBASE]  c:0:0: Realtime connection established. 
main.js?1c90:62 [FIREBASE]  p:0: connection ready 

[FIREBASE]  p:0: Listen on /details for default

......
main.js?1c90:62 [FIREBASE]  p:0: handleServerMessage d {"p":"details","d":{"address1":"3, chemin des buis","address2":"Les Ballans","city":"Mornac","country":"France","familyName":"Dufour","givenName":"Yves","honorificPrefix":"Mr.","uid":"mayU7Zyc3GMC4STXB6mVu32xFO13","zipcode":"16600"}} 

main.js?1c90:62 [FIREBASE]  event: /details:value:{"address1":"3, chemin des buis","address2":"Les Ballans","city":"Mornac","country":"France","familyName":"Dufour","givenName":"Yves","honorificPrefix":"Mr.","uid":"mayU7Zyc3GMC4STXB6mVu32xFO13","zipcode":"16600"} 

[FIREBASE]  event: /details:value:null 
 SNAPSHOT VAL  null

我的查询出了什么问题? (这是我的第一个。...找不到匹配的好例子...)

更新

我也尝试了以下查询

payload.db.ref('details').orderByChild('uid').equalTo(payload.uid).once('value', (snapshot) => { 

但是我得到一个错误

    vue.runtime.esm.js?ff9b:1737 Error: Query.equalTo failed: First argument contains undefined in property 'details'
        at validateFirebaseData (index.cjs.js?8c4d:1432)
        at validateFirebaseDataArg (index.cjs.js?8c4d:1420)
        at Query.equalTo (index.cjs.js?8c4d:5004)
        at Store.getUserDetail (root.js?f07d:132)
        at Array.wrappedActionHandler (vuex.esm.js?358c:704)
        at Store.dispatch (vuex.esm.js?358c:426)
        at Store.boundDispatch [as dispatch] (vuex.esm.js?358c:332)
        at VueComponent.userDetails (eval at ./node_modules/cache-loader/dist/cjs.js?{"cacheDirectory":"/Users/yves/Developments/WIP/VUE.JS-vcli-3-beta/le-choro-des-charentes/node_modules/.cache/cache-loader"}!./node_modules/babel-loader/lib/index.js!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./src/components/Member/Section1.vue (1.js:7), <anonymous>:154:19)

更新2

我更新了数据库结构,请使用user.uid作为用户的密钥。详细信息

新的数据库结构

    "details": {
        "mayU7Zyc3GMC4STXB6mVu32xFO13": {
         "honorificPrefix": "Mr.",
         "givenName": "Yves",
         "familyName": "Dufour",
         "address1": "3, chemin des buis",
         "address2": "Les Ballans",
         "zipcode": "16600",
         "city": "Mornac",
         "country": "France"
        },
        "kRdsicIfuRYSTj2sWxfejhMevtr2" : {
         "honorificPrefix": "Mr.",
         "givenName": "John",
         "familyName": "Doe",
         "address1": "829, Flower Avenue",
         "address2": "Dizzy Gong",
         "zipcode": "CA 90291",
         "city": "Venice",
         "country": "USA"
        }
        }
    }

所以现在我需要基于user.id作为键来创建一个user.detail对象

我尝试了

 payload.db.ref('details').child(payload.uid).once('value', (snapshot) => {
     console.log('SNAPSHOT VAL ', snapshot.val())
 })

但没有成功...

当我查询familyName时,我可以正常运行

payload.db.ref('details').orderByChild('familyName').equalTo('Doe').once('value', (snapshot) => {.. }

2 个答案:

答案 0 :(得分:0)

应该为.equalTo(payload.uid),而不是equalTo('uid', payload.uid)

   payload.db.ref('details').orderByChild('uid').equalTo(payload.uid).once('value', (snapshot) => {
     // db.ref('details').orderByChild('userId').once('value', (snapshot) => {
     console.log('SNAPSHOT VAL ', snapshot.val())
   })

答案 1 :(得分:0)

已解决

具有更新的结构(使用user.uid作为键,我需要使用

payload.db.ref('details').orderByKey().equalTo(payload.uid).once('value', (snapshot) => {... }

如果我想查询一个值(即familyName),我将使用:

payload.db.ref('details').orderByChild('familyName')
       .equalTo('Doe').once('value', (snapshot) => {... }