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

253
Visualizações
I am having problems getting an async function to finish running before submitting form

I am trying to get an image URL from firebase storage before submitting a form to firestore using formik I am new to programming and I have been trying for days. This is the code to grab the URL

let uploadImage = async (uri, imageName) => {
const response = await fetch(uri);
const blob = await response.blob();
const storageRef = firebase.storage().ref("images/" + imageName);

storageRef.put(blob).then((snapshot) => {
  snapshot.ref.getDownloadURL().then((downLoadURL) => {
    console.log("file available at: ", downLoadURL);
    //setImageStorage(null);
    setImageStorage({ photo: downLoadURL });
    
  
  })
});

};

bellow is the code to set the URL in state

const storageImage =  () => {
if (selectedImage !== null) {
   uploadImage(selectedImage.localUri, randomName)

} else {
  setImageStorage({ photo: defaultImage });
}

};

function submitForm(values) {
db.collection("item")
 .add({
    values,
    category: category,
    image: imageStorage.photo,
    latitude: newLatitude,
    longitude: newLongitude,
 }

code in formik looks like this

<Formik
    initialValues={{ description: "", price: "", roomName: "" }}
    validationSchema={itemSchema}
    onSubmit={ async (values, actions) => {
     
     await storageImage().then((()=>{
        submitForm(values);
     }))
      
      

      actions.resetForm();
      setCategory("");
      setSelectedImage(null);

      setImageStorage(null);

      navigation.navigate("YardSale");

I am getting the following error: Warning: An unhandled error was caught from submitForm(), [TypeError: null is not an object (evaluating 'imageStorage.photo')]

Please help

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

await storageImage().then((()=>{
        submitForm(values);
}))  // here values get added to the database before uploading image

uploadImage

let uploadImage = async (uri, imageName) => {
    const response = await fetch(uri);
    const blob = await response.blob();
    const storageRef = firebase.storage().ref("images/" + 
    imageName);
    
    const snapshot = await storageRef.put(blob){
    const downloadUrl = await snapshot.ref.getDownloadURL()
    console.log("file available at: ", downLoadURL);
    setImageStorage({ photo: downLoadURL });
}

storageImage

const storageImage = async () => { // added async
    if (selectedImage !== null) {
       await uploadImage(selectedImage.localUri, randomName) // added await
    } else {
      setImageStorage({ photo: defaultImage });
    }
}

In Formik

<Formik
    initialValues={{ description: "", price: "", roomName: "" 
    }}
    validationSchema={itemSchema}
    onSubmit={ async (values, actions) => {

     // you shouldn't use await and then togather
     await storageImage();
     submitForm(values);
     
...
about 4 years ago · Santiago Gelvez 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