Uploading multiple images to firebase collection and firebase using react native expo

34 views Asked by At

Am trying to uploading multiple images to firebase storage and firebase database collection using react native 'expo' but it's giving a hard time, i even tried using chatGpt but the code generated is confusing and somehow outdated so its not workingenter image description here

enter image description here

enter image description here enter image description here

1

There are 1 answers

0
Mauro Russo On

I wrote a function for this. Here's:

import { ref, uploadBytes, getDownloadURL } from 'firebase/storage'
import { auth, storage } from '../../config/firebase'
export async function uploadImage(uri) {
try {
    const response = await fetch(uri)
    const blobFile = await response.blob()

    const image_name = 'image_name'
    const metadata = {
        contentType: 'image/jpeg',
        customMetadata: {
            from: auth?.currentUser?.uid
        }
    }

    const reference = ref(storage, image_name)
    const result = await uploadBytes(reference, blobFile, metadata)
    const url = await getDownloadURL(result.ref)

    return url
} catch (err) {
    return Promise.reject(err)
}
}