[英]How to upload an image to firebase storage and then return the download url?
Im trying to upload an image to firebase storage in a react-native project, then return the download URL once upload is complete.我尝试将图像上传到 react-native 项目中的 firebase 存储,然后在上传完成后返回下载 URL。
this is what I tried: EDITED:这就是我尝试过的:编辑:
const uploadImg = async (dispatch, uri, uid, mime = 'image/png') => {
console.log('Starting image upload...');
dispatch({ type: types.UPLOAD_IMG, info: 'Uploading profile image...' });
const uploadUri = Platform.OS === 'ios' ? uri.replace('file://', '') : uri;
return (imageRef = firebase
.storage()
.ref(uid)
.child('profileImg')
.putFile(uploadUri, { contentType: 'image/png' })
.catch((err) => {
uploadImgFail(dispatch, err.message);
}));
};
part of implementation:部分实施:
uploadImg(dispatch, img, userCredential.user.uid)
.then((imgRef) => {
const imgUrl = imgRef.getDownloadURL();
console.log('uploaded Image URL: ' + imgUrl)
uploadImgSuccess(dispatch, 'Profile image upload successful...');
// rest of action
}
the problem is the download URL isn't returned by the function, I get unhandled promise rejection: "can't find variable: imgUrl"问题是该函数没有返回下载 URL,我得到未处理的承诺拒绝:“找不到变量:imgUrl”
Any help on achieving this with react-native-firebase
?使用react-native-firebase
实现这一目标有什么帮助吗?
You will need to actually return a value from uploadImg
.您实际上需要从uploadImg
返回一个值。 Right now it's returning nothing.现在它什么都不返回。 Add a return
keyword before imageRef.putFile()
to return the promise that its chained promise returns.在imageRef.putFile()
之前添加return
关键字以返回其链接的承诺返回的承诺。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.