0

I tried few things with this function but I keep getting a fulfilled undefined promise.

Here is what I am trying to achieve:

I have a React component that stores an image file in Firebase Storage, on that same function I want to fetch back the image URL so I could set the user object with that url string.

My Component function:

  const uploadImage = ( user:any, imageFile: FileList) => {
      setUserData({
        ...userData,
        imageUrl: uploadUserImage(imageFile[0], user.uid),
      });
      console.log(userData.imageUrl)
  };

The uploadUserImage is a function in my user controller:

export const uploadUserImage = async (image: File, userId: string) => {
  const imageRef = ref(storage, `images/${userId}`);
  uploadBytes(imageRef, image).then((snapshot) => {
     getDownloadURL(snapshot.ref).then((url) => {
      console.log(url);
         url.toString();
    });
  });
};

I'm not sure why, but everything works fine until the return of the URL, I can console.log the URL string, but it returns undefined fulfilled promise on my component. enter image description here
I tried few things, like giving an implicit return to my uploadUserImage function, and added conversion to a string of the URL (but not sure if it's needed).

I saw online few examples, but they all did it inside the react component where the promise result was set to a state. What am I doing wring here ?

dro dro
  • 21
  • 2
  • You are missing `return` or `await` in your function. Take a look at [this answer](https://stackoverflow.com/a/30180679/3068190) and the rest of the thread I attached to your question for more details. – samthecodingman Mar 28 '23 at 00:23

0 Answers0