Javascript React本机Firebase多图像上载问题
在React Native中,我使用ImagePicker包选择图像并将图像上载到Firebase存储。我还想使用带有Firebase Rest Api的Getdownloadurl()方法发送Firebase存储映像链接 我设法上传图像,但当我通过发出rest api post请求发送图像时,图像链接将未定义。我在控制台屏幕上发出post请求,但没有100%的总图像大小Javascript React本机Firebase多图像上载问题,javascript,typescript,react-native,firebase-storage,Javascript,Typescript,React Native,Firebase Storage,在React Native中,我使用ImagePicker包选择图像并将图像上载到Firebase存储。我还想使用带有Firebase Rest Api的Getdownloadurl()方法发送Firebase存储映像链接 我设法上传图像,但当我通过发出rest api post请求发送图像时,图像链接将未定义。我在控制台屏幕上发出post请求,但没有100%的总图像大小 const uploadImage = async (uploadUri: string, transferred: num
const uploadImage = async (uploadUri: string, transferred: number) => {
let filename = uploadUri.substring(uploadUri.lastIndexOf('/') + 1);
transferred = 0;
const storageRef = storage().ref(`images/${filename}`);
const task = storageRef.putFile(uploadUri);
task.on('state_changed', taskSnapshot => {
console.log(`${taskSnapshot.bytesTransferred} transferred out of ${taskSnapshot.totalBytes}`);
transferred = Math.round((taskSnapshot.bytesTransferred / taskSnapshot.totalBytes) * 100);
});
try {
await task;
const url = await storageRef.getDownloadURL();
return url;
} catch (error) {
console.log(error);
return null;
}
}
export const addPost = (files: string[], post: Post): ThunkAction<void, RootState, null, AddPostAction> => {
return async dispatch => {
try {
dispatch({ type: ADD_POST_LOADING, payload: true });
const imageData: Image[] = [];
files.map(async (item) => {
const imageUrl = await uploadImage(item, 0);
imageData.push({ image: imageUrl! });
});
const postData: Post = {
...post,
thumb: imageData[0].image
}
const response = await fetch(`${BASE_URL}/post.json`, { method: "POST", body: JSON.stringify(postData) });
if (response.ok) {
const successPostData: AddPostSuccess = await response.json();
dispatch({ type: ADD_POST_SUCCESS, payload: successPostData });
} else {
dispatch({ type: ADD_POST_ERROR, payload: "404" });
}
} catch (error) {
console.log(error);
}
}
}
const uploadImage=async(uploadUri:string,transfer:number)=>{
让filename=uploadUri.substring(uploadUri.lastIndexOf('/')+1);
转移=0;
const storageRef=storage().ref(`images/${filename}`);
const task=storageRef.putFile(上传URI);
task.on('state_changed',taskSnapshot=>{
log(`taskSnapshot.bytesttransferred}从${taskSnapshot.totalBytes}中转出);
transferred=Math.round((taskSnapshot.ByTestTransferred/taskSnapshot.totalBytes)*100);
});
试一试{
等待任务;
const url=wait-storageRef.getDownloadURL();
返回url;
}捕获(错误){
console.log(错误);
返回null;
}
}
export const addPost=(文件:string[],post:post):ThunkAction=>{
返回异步调度=>{
试一试{
分派({type:ADD\u POST\u LOADING,payload:true});
常量imageData:Image[]=[];
file.map(异步(项)=>{
const imageUrl=wait uploadImage(项,0);
push({image:imageUrl!});
});
常量postData:Post={
邮递
thumb:imageData[0]。图像
}
const response=wait fetch(`${BASE_URL}/post.json`,{method:“post”,body:json.stringify(postData)});
if(response.ok){
const successPostData:AddPostSuccess=wait response.json();
分派({type:ADD_POST_SUCCESS,payload:successPostData});
}否则{
分派({type:ADD_POST_ERROR,payload:“404”});
}
}捕获(错误){
console.log(错误);
}
}
}
Amap
无法处理async
代码。尝试更改此部分:
files.map(异步(项)=>{
const imageUrl=wait uploadImage(项,0);
push({image:imageUrl!});
});
为此:
for(设i=0;i