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

360
Visualizações
Vue 3 Firebase 9 Vuex, in actions get URL of file after uploading to storage

Hi I am trying to get the url of an image after uploading to Firebase storage. In the commented code below I show a few things I have tried. I have successfully uploaded an image to Firebase Storage.

async uploadAvatar({ state }, { authId, file, filename }) {
      if (!file) return null
      authId = authId || state.authId
      filename = filename || file.name
      try {
        const storage = getStorage()
        const storageRef = ref(
          storage,
          `uploads/${authId}/images/${Date.now()}-${filename}`
        )
        // This is 1st way I tried
        // This sucessfully uploads image to storage
        // uploadBytes(storageRef, file)
        // // const snapshot = await uploadBytes(storageRef, file)
        // This throws a not found error
        // const url = await getDownloadURL(storageRef)
        // console.log('URL', url)
        // return url

        // // This sucessfully uploads image to storage
        uploadBytes(storageRef, file).then((snapshot) => {
          console.log('Uploaded avatar!')
          const avatarUrl = getDownloadURL(storageRef)
            .then((url) => {
              // This is correct url
              console.log('URL', url)
              return url
            })
            .catch((err) => {
              console.log('Error with file url', err)
            })
          // This logs promise pending with correct url inside
          console.log('avatarUrl', avatarUrl)
          return avatarUrl
        })
      } catch (error) {
        const { addNotification } = useNotifications()
        addNotification({
          message: 'Error uploading avatar image',
          type: 'error'
        })
      }
    }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You're not returning anything from the top-level code in your uploadAvatar function yet.

async uploadAvatar({ state }, { authId, file, filename }) {
  if (!file) return null
  authId = authId || state.authId
  filename = filename || file.name
  try {
    const storage = getStorage()
    const storageRef = ref(
      storage,
      `uploads/${authId}/images/${Date.now()}-${filename}`
    )

    // 👇
    return uploadBytes(storageRef, file).then((snapshot) => {
      console.log('Uploaded avatar!')
      const avatarUrl = getDownloadURL(storageRef)
        .then((url) => {
          // This is correct url
          console.log('URL', url)
          return url
        })
        .catch((err) => {
          console.log('Error with file url', err)
        })
      // This logs promise pending with correct url inside
      console.log('avatarUrl', avatarUrl)
      return avatarUrl
    })
  } catch (error) {
    const { addNotification } = useNotifications()
    addNotification({
      message: 'Error uploading avatar image',
      type: 'error'
    })
  }
}

Honestly though, the whole code that handles the download URL in here seems unnecessary, so I recommend simplifying to:

async uploadAvatar({ state }, { authId, file, filename }) {
  if (!file) return null
  authId = authId || state.authId
  filename = filename || file.name
  try {
    const storage = getStorage()
    const storageRef = ref(
      storage,
      `uploads/${authId}/images/${Date.now()}-${filename}`
    )

    return uploadBytes(storageRef, file).then((snapshot) => {      
      return getDownloadURL(storageRef);
    })
  } catch (error) {
    const { addNotification } = useNotifications()
    addNotification({
      message: 'Error uploading avatar image',
      type: 'error'
    })
  }
}
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