Home > front end >  Having trouble using database data in vue app?
Having trouble using database data in vue app?

Time:08-25

Hi I've been trying to get multiple profiles from a database and use v-for to try and display them all but whenever I try it doesn't work on and I can't figure out how to get it to use the data which should be getting imported. Below is what the javascript file looks like.

import { projectFirestore } from "../Firebase/Config";
import { ref } from "vue"

const getPremium = () => {
    const Premium = ref([])
    const error = ref(null)

    const load = async () => {
        try{
            const res = await projectFirestore.collection('Premium').get()

            Premium.value = res.docs.map(doc => {
               console.log(doc.data())
               return {...doc.data(), id: doc.id}
            })
        }
        catch (err){
            error.value = err.message
            console.log(error.value)
        }
    }

    return { Premium, error, load}
}

export default getPremium

And heres the vue where I am trying to use v-for to create the profiles.

<script>
import getPremium from "../Composables/getPremium.js";
const {Premium, error, load} = getPremium();
load();
</script>
<template>
<div v-for =" Premium in getPremiums" :key="Premium.id" >
 <div class= "hover:scale-105 transition ease-in-out duration-300 bg-neutral-800 hover:bg-neutral-900 active:bg-neutral-900 text-neutral-400 font-bold rounded-xl">
  <br>
     <p>{{ Premium.Name }}</p>
     <img src="../assets/Sample-pic.png" >
     <div >
     <p>Location:</p>
     <p>{{ Premium.Location }}</p>
     <p>Rates:</p>
     <p>{{ Premium.Rates }} /hr</p>
     <p>Phone:</p>
     <p>{{ Premium.Phone }}</p>
   
    </div><br>
  </div>
  </div>

</template>

I'm not sure what I need to do from here to get it to work properly, I'm new to using databases and any help would be greatly appreciated, Thankyou

CodePudding user response:

Besides, you are trying to iterate through function, you iterate it using in operator. In operator iterates object properties, no arrays. And created variables call using first small character not big one like you have in code "Premium".

<script>
  import getPremium from "../Composables/getPremium.js";
  const { premiumList, error, load } = getPremiumList(); // Name variables, functions with first small letter not big one. Interfaces, Clases with first big letter. Add "List" or "s" in end of name so everyone knows it is array.
  load();
</script>
<template>
    <main>
        <div v-for="item of premiumList" :key="item.id">
            <div
                
            >
                <br />
                <p>{{ item.Name }}</p>
                <img
                    src="../assets/Sample-pic.png"
                    
                />
                <div >
                    <p>Location:</p>
                    <p>{{ item.Location }}</p>
                    <p>Rates:</p>
                    <p>{{ item.Rates }} /hr</p>
                    <p>Phone:</p>
                    <p>{{ item.Phone }}</p>
                </div>
                <br />
            </div>
        </div>
    </main>
</template>

CodePudding user response:

You are destructuring Premium from getPremium(), so that is what you should use in your v-for


    <script>
import getPremium from "../Composables/getPremium.js";
const { Premium, error, load } = getPremium();
load();
</script>
<template>
    <main>
        <div v-for="item in Premium" :key="item.id">
            <div
                
            >
                <br />
                <p>{{ item.Name }}</p>
                <img
                    src="../assets/Sample-pic.png"
                    
                />
                <div >
                    <p>Location:</p>
                    <p>{{ item.Location }}</p>
                    <p>Rates:</p>
                    <p>{{ item.Rates }} /hr</p>
                    <p>Phone:</p>
                    <p>{{ item.Phone }}</p>
                </div>
                <br />
            </div>
        </div>
    </main>
</template>


  • Related