Retrieve Firebase data in Vuejs with Vuefire

2019-06-14 04:20发布

问题:

I have my firebase DB with this tree structure.

I use vue-fire to loop through the database. I want to retrieve the "CantFindMe" value in the name property. My files look like this:

// Dashboard.vue
<template>

<div class="">
  <ul>
    <li v-for="personName in names" v-bind:key="personName['.key']">
      <div v-if="!personName.edit">

        <p>{{ personName.forms.step1 }}</p>

        </div>
    </li>
    </ul>
</div>
</template>

<script>

import { namesRef} from '../firebase.js'

export default {
  name: 'app',
  data () {
    return {
      name: ''
    }
  },
  firebase: {
    names:  namesRef
  }
}
</script>

// Firebase.js

  { The usual firebase config }
  {...}
  export const firebaseApp = firebase.initializeApp(config)
  export const db = firebaseApp.database()
  export const namesRef = db.ref('names') 

I manage to read the object: { "-KxrySGBHgLvw_lPPdRA": { "edit": false, "name": "CantFindMe" } }

But when I try to add ".name" after ".step1", that should supposedly return "CantFindMe", I get nothing/blank back.

How do I get to the name property using VueJs to return "CantFindMe"?

回答1:

Sorry for delay, Im not using vuefire... So, first - do not refer to names only, but directly to step1:

export const namesRef = db.ref('names/EcoClim/forms/step1/')

You will obtain structure like this:

[{
  ".key": "-KxrySGBHgLvw_lPPdRA",
  "edit": "false",
  "name": "CantFindMe"
},  {
  ...
}]

Now you can use it in template, but as key, refer to array index and not to FBase key:

<li v-for="(person, idx) in names" :key="idx">
  <div v-if="!person.edit">
    <p>{{ person.name }}</p>
  </div>
</li>