Я успешно загрузил файлы в хранилище firebase, но по иронии судьбы, URL-адрес какого-то файла не сохранился в базе данных firestore. Я попытался записать результат. Все URL есть, но, как я уже сказал, только некоторые из них были сохранены в базе данных пожарного магазина.
uploadFiles = (item) => {
item.uploadTask.on('state_changed', snapshot => {
totalBytes = snapshot.totalBytes
// Observe state change events such as progress, pause, and resume
// Get task progress, including the number of bytes uploaded and the total number of bytes to be uploaded
var progress = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
const info = {
id: item.id,
transfer_size: this.formatFileSize(snapshot.bytesTransferred),
total_size: this.formatFileSize(snapshot.totalBytes),
progress: String(parseInt(progress)+'%')
}
this.props.updateFile(info)
switch (snapshot.state) {
case firebase.storage.TaskState.PAUSED: // or 'paused'
break;
case firebase.storage.TaskState.RUNNING: // or 'running'
console.log('Upload is running');
break;
}
}, error => {
// Handle unsuccessful uploads
switch(error.code){
case 'storage/canceled':
alert('canceled')
break;
}
}, () => {
//Store url in the firestore
item.uploadTask.snapshot.ref.getDownloadURL().then((downloadURL) => {
console.log(downloadURL)
addDoc(this.props.car_id, downloadURL, item.type, item.file_name, this.fileExtension(item.type), totalBytes, item.id, [...new Set([...this.props.folder_list ,...this.props.docs_list])]).then(res => {
this.props.removeUploadFile(item.id) // remove item in the list after successful upload
})
});
});
}
export const addDoc = (car_id, url, type, file_name, ext, file_size, id, docs_list) => {
return new Promise((resolve, reject) => {
const email = firebase.auth().currentUser.email;
const user = firebase.firestore().collection('users');
const cars = user.doc(email).collection('cars');
if(!docs_list.some(item => item.file_name === file_name) || docs_list.length === 0)
docs_list.push({ id, url, type, file_name, file_size, ext, date_uploaded: new Date() })
cars.doc(String(car_id)).update({
docs: docs_list
}).then(res => {
resolve('success')
}).catch(err => {
reject(JSON.stringify(err))
})
})
}