0

I would like to show data in template when the page loads after i fetch the data from a database. The data is returned with async/await. The select menus I use for filtering are now reactive only once I select a value from the menu. But I would like the full(unfiltered) data array to show when the page first loads and before any filters are applied. How can I wait untill the data is fully fetched besore passing it to my template ?

<template>
  <div class="mp" style="width: 100vw;">
    <div class="col-2">
                <span class="lbl">FILTER1</span>
                <select v-model="selectedMA" class="form-select"  >
                <option value="">all</option>
                <option value="Germany">Germany</option>
                </select>
    </div>
    <div class="col-2">
                <span class="lbl">FILTER2</span>
                <select v-model="selectedMA2" class="form-select"  >
                <option value="">all</option>
                <option value="Tier1">Tier1</option>
                </select>
    </div>

    <p>DATA :</p>
    <p> {{actorListTest2}} </p>
  </div>  
</template>


<script >
import {watchEffect, ref, computed} from 'vue'
import getActorDocs from '../composables/getActorDocs'
export default {
setup(){

  const {actorDocs, loadActors} = getActorDocs()
  const selectedMA = ref("")
  const selectedMA2 = ref("")
  
  loadActors();
  var filteredActors = actorDocs

  const actorListTest2 = computed(() => {
    if(selectedMA == null && selectedMA2 == null){return filteredActors}
    if(selectedMA.value == "" && selectedMA2.value == ""){return filteredActors}
    else { 
      return filteredActors.filter(obj => {
        return (!selectedMA2.value.length || obj.type == selectedMA2.value)
        && (!selectedMA.value.length || obj.country == selectedMA.value)
    })}
    
    });
  watchEffect(() => console.log(actorListTest2.value))

    return {filteredActors, actorListTest2, selectedMA, selectedMA2, actorDocs}
    }//setup
}
</script>

As requested, here is getActorDocs():

import {ref} from 'vue'
import { projectFirestore } from '../firebase/config'
import { collection, getDocs } from "firebase/firestore";

const getActorDocs = () => {
    const actorDocs = []
    const error = ref(null)
  

    const loadActors = async () => {
        try {
            const querySnapshot = await getDocs(collection(projectFirestore, "actors"));
            querySnapshot.docs.map(doc => {
                actorDocs.push(doc.data())
            }) 
        } catch (err) {
            error.value = err.message
            console.log(error.value)
        }
    }
    
    return { actorDocs, error, loadActors} 
}
export default getActorDocs
JK2018
  • 429
  • 1
  • 9
  • 23
  • please share the code of `getActorDocs` – Boussadjra Brahim Jan 12 '22 at 14:35
  • sounds like `getActorDocs` is async and you are facing a race condition. – Ergec Jan 12 '22 at 14:37
  • You misspelled "thaData" in your template. – Christopher Shroba Jan 12 '22 at 14:43
  • You are right Ergec, getActorDocs uses async / await. – JK2018 Jan 12 '22 at 14:59
  • 1
    This has already been answered in your previous question, https://stackoverflow.com/questions/70665334/vue-3-composition-api-passing-data-and-making-it-reactive . The problem and the solution didn't change since then. – Estus Flask Jan 12 '22 at 15:29
  • I have used computed in order to make my filtering reactive as you suggested in my other post. But I can't figure out how to get the unfiltered data to show on initial page load. This is probably very obvious to many, but not for Vue noobie like myself – JK2018 Jan 12 '22 at 16:26
  • @JK2018 actorListTest2 can stay the way you did,, but looks like unfilter condition was messed up, it should be just `if (!selectedMA.value && !selectedMA2.value) return actorDocs`. And the rest of the code needs to be like I suggested. actorDocs needs to be reactive, it won't work as `const actorDocs = []`. Btw `@` needs to be used on SO in order for users to receive notifications – Estus Flask Jan 13 '22 at 06:39

0 Answers0