Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
Trying to return a random post with Vue 3 composition API and Firebase

Trying to wrap my head around the composition api, but apparently need a bit of help. Loading in all my "Quotes" from a Firebase DB with this

import { ref } from 'vue'
import { projectFirestore } from '../firebase/config'

const getQuotes = () => {
  const quotes = ref([])
  const error = ref(null)

  const load = async () => {
    try {
      const res = await projectFirestore.collection('quotes').get()
      
      quotes.value = res.docs.map(doc => {
        return { ...doc.data(), id: doc.id }
      })
    }
    catch (err) {
      error.value = err.message
      console.log(error.value)
    }
  }

  return { quotes, error, load}
}

export default getQuotes

Which works as intended. But then I try to create a filter so only 1 Quote is shown randomly like this:

<template>
  <section class="home">
    <h1 style="color: pink">{{ randomQuote }}</h1>
    <Quote :quote="quote" v-for="quote in quotes" :key="quote.id" />
  </section>
</template>

<script>
import getQuotes from '@/composables/getQuotes'
import Quote from '@/components/Quote.vue'
import { computed } from '@vue/reactivity'

export default {
  name: 'Home',
  components: { Quote },
  setup() {
    const { quotes, error, load } = getQuotes()
    const randomQuote = computed(() => {
      return quotes.Math.floor(Math.random() * quotes.length)
    })

    load()

    return { randomQuote, quotes, error }
  }
}
</script>

In my head this should work... anyone who can spot the error and give me a heads up?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This syntax means quotes.Math.floor(Math.random() * quotes.length) that a property named Math on quotes array, while it's Math global variable that is supposed to be accessed. Since an array doesn't have such property, this will result in error. If the intention is to specify dynamic array index, it cannot be differed from accessing a property.

In order to access an index, bracket notation should be used, likely:

quotes[Math.floor(Math.random() * quotes.length)]
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda