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

119
Visualizações
Getting the download URL of an uploaded image synchronously in Firebase Storage

I'm using Firestore and Storage in a React Native project and I'm working on a feature that migrates SQLite data into Firebase. Once the user signs in for the first time, the migration process runs synchronously. Among the data to migrate there are base64 images and no matter what I do, despite using async/await everywhere, I get the DownloadURL "too late", after the migration process is done and the new data from Firestore is listed. So, I cant show the images at first, I have to reload the app.

My code:

const upload64BaseImage = async (photo, docRef) => {
  try {
    //fetch doesn't work with base64 images and Firebase Cloud Storage
    const blob = await new Promise((resolve, reject) => {
      const xhr = new XMLHttpRequest();
      xhr.onload = function () {
        resolve(xhr.response);
      };
      xhr.onerror = function (e) {
        console.log(e);
        reject(new TypeError("Network request failed"));
      };
      xhr.responseType = "blob";
      xhr.open("GET", photo, true);
      xhr.send(null);
    });

    const imageRef = ref(storage, `images/${docRef}.jpg`);
    const metadata = {
      contentType: "image/jpeg",
    };

    const snapshot = await uploadBytes(imageRef, blob, metadata);
    const downloadUrl = await getDownloadURL(snapshot.ref);
    
    return downloadUrl;
  } catch (error) {
    console.log(`error`, error.message);
  }
};

const createNewDocument = async (userId, data) => {
  try {
    const newUserRef = doc(collection(db, "users"));
    const myTimestamp = Timestamp.fromDate(new Date(data.Birthdate));
    const downloadUrl = await upload64BaseImage(data.Photo, newUserRef.id);

    //crear un objeto
    const newUserObjetc = {
      UserId: userId,
      Birthdate: myTimestamp,
      Name: data.Name,
      Surname: data.Surname,
      Photo: downloadUrl || null,
    };

    await setDoc(newUserRef, newUserObjetc);

    return newUserRef.id;
  } catch (error) {
    console.log(`error`, error.message);
  }
};
about 4 years ago · Juan Pablo Isaza
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