如果不存在,我正在尝试创建一个集合并上传 img 文件。
我正在接收上传图像 URL 和进度,但它没有创建名为图像的集合。
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;
不确定是否addDoc()
有效。