首页 > 解决方案 > Vue.js 。Firebase 查询 - 如何从查询中获得结果?

问题描述

我有一个只有一个测试值的 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 作为 user.details 的键

新的数据库结构

    "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"
        }
        }
    }

所以现在我需要 gte 基于 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) => {.. }

标签: firebasefirebase-realtime-databasevue.jsvuex

解决方案


解决了

随着更新的结构(使用 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) => {... }

推荐阅读