【发布时间】:2019-07-02 11:26:15
【问题描述】:
我正在将图像上传到 firebase 并从中获取 imageUrl,但问题是我无法等待特定调用结束并在获取 imageURL 之前执行该过程
我也尝试过 Promise 和 Async 函数等待,但问题没有解决
下面是我的 js 文件,其中首先调用 addItem ,然后我将图像上传到 firebase 并且该 URL 想要推送到 firebase 数据库
import { db,fireBaseObj } from '../firebase/db';
import RNFetchBlob from 'react-native-fetch-blob';
export const addItem = (userId,title,description,isdone,PriorityIndex,PriorityValue,image_path) => {
uploadImage(image_path) // Here is my upload image function
db.ref('/items/'+userId+'/').push({
title: title,
description: description,
isdone: isdone,
PriorityIndex:PriorityIndex,
PriorityValue:PriorityValue,
}).then(res =>{
return true;
}).catch(error =>{
return false;
})
}
export const uploadImage = (image_path) => {
const Blob = RNFetchBlob.polyfill.Blob;
const firestore = RNFetchBlob.fs;
window.XMLHttpRequest = RNFetchBlob.polyfill.XMLHttpRequest;
window.Blob = Blob;
const imageName = image_path.path.substring(image_path.path.lastIndexOf("/")+1);
let uploadBlob = null;
const imageRef = fireBaseObj.storage().ref("ref").child(imageName);
const mime = 'image/jpg';
firestore.readFile(image_path.path, 'base64')
.then((data) => Blob.build(data, { type: `${mime};BASE64` })
)
.then((blob) => {
uploadBlob = blob;
return imageRef.put(blob, { contentType: mime });
})
.then(() => {
uploadBlob.close();
return imageRef.getDownloadURL();
})
.then((url) => {
const obj = {};
obj.loading = false;
obj.dp = url;
this.setState(obj);
return url;
})
.catch((error) => {
console.log(error);
return error;
});
}
任何帮助将不胜感激,因为我没有得到如何处理这种情况的确切路径
【问题讨论】:
标签: firebase react-native redux react-redux firebase-storage