1

I'm trying to create a collection if not exist and uploading img file.

I'm receiving upload image URL and progress to but it is not creating collection named images.

const useStorage = (file) => {
const [progress, setProgress] = useState(0);
const [error, setError] = useState(null);
const [url, setUrl] = useState(null);

useEffect(() =>{
const storageRef = ref(storage, file.name);
const collectionRef = collection(db, "images");

console.log(collectionRef)


//get Progress
const uploadTask = uploadBytesResumable(storageRef, file);
uploadTask.on('state_changed', 
(snap) => {
const progress = Math.round((snap.bytesTransferred / snap.totalBytes) 
* 100);
console.log('Upload is ' + progress + '% done');
setProgress(progress);
}, 
(error) => {
setError(error)
}, 

// Handle successful uploads on complete
async () => {
const url= await getDownloadURL(uploadTask.snapshot.ref);
console.log('File available at', URL);
collectionRef.addDoc({ url});
setUrl(URL);
});
},[file])
return { progress, error, url };
}

export default useStorage;

Not sure if addDoc() works.

Digamber negi
  • 407
  • 1
  • 7
  • 20

3 Answers3

1

Import : import {collection,addDoc} from 'firebase/firestore';

Replace : collectionRef.addDoc({ url});

To : await addDoc(collectionRef, { url});

Digamber negi
  • 407
  • 1
  • 7
  • 20
0

Collections are just appear and disappear. If you delete all files in collection, collection it self will gone. If you create any document in not existing collection, collection will exist.

Mises
  • 4,251
  • 2
  • 19
  • 32
-1

The problem your facing is with your emulator. Try both.

  1. Cold boot your emulator
  2. Try in real device

I also had same issue and fixed it using one of two steps. :)

Afnan
  • 5
  • 3
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 01 '23 at 21:48