【发布时间】:2020-04-01 16:04:54
【问题描述】:
所以我正在为数组中的多个图像开发上传功能。经过一番努力,我终于让我的上传功能正常工作,并且图像显示在 Firebase 数据库中。但是,我还没有找到一种工作方法来确保我的上传功能在继续之前完成。
下面是我调用上传函数并尝试将响应存储在uploadurl中的部分,uploadurl变量稍后在调度函数中用于存储url和其他数据。
try {
uploadurl = await uploadImages()
address = await getAddress(selectedLocation)
console.log(uploadurl)
if (!uploadurl.lenght) {
Alert.alert('Upload error', 'Something went wrong uploading the photo, plase try again', [
{ text: 'Okay' }
]);
setIsLoading(true);
return;
}
dispatch(
所以图片上传功能如下。这适用于图像已上传,但是获取 DownloadURL 的 .then 调用未正确启动,并且 .then 图像也无法正常工作。
uploadImages = () => {
const provider = firebase.database().ref(`providers/${uid}`);
let imagesArray = [];
try {
Promise.all(photos)
.then(photoarray => {
console.log('all responses are resolved succesfully')
for (let photo of photoarray) {
let file = photo.data;
const path = "Img_" + uuid.v4();
const ref = firebase
.storage()
.ref(`/${uid}/${path}`);
var metadata = {
contentType: 'image/jpeg',
};
ref.putString(file, 'base64', metadata).then(() => {
ref
.getDownloadURL()
.then(images => {
imagesArray.push({
uri: images
});
console.log("Out-imgArray", imagesArray);
})
})
};
return imagesArray
})
} catch (e) {
console.error(e);
}
};
所以我想在所有照片上传之后返回 imagesArray。那么在第一个函数中将 imagesArray 设置为 uploadURL 呢?在 imagesArray 中设置所有图像 URL 并传递给 uploadURL 之后,我的调度函数才能继续上传其余数据。如何确保这按预期进行?
我现在已经改变了很多次,因为我不断地被发送到不同的方式来做这件事,我完全不知道现在如何继续:(
【问题讨论】:
-
在调用者中使用
let photoarray = await Promise.all(photos);并将photoarray传递给uploadImages()会更好。此外,uid和uuid应该被传递(如果它们确实是常量)。
标签: firebase function react-native promise image-uploading