如何将图像上传到 firebase 存储然后 return 下载 url?

How to upload an image to firebase storage and then return the download url?

我正在尝试将图像上传到 react-native 项目中的 firebase 存储,然后 return 下载 URL 上传完成后。

这是我尝试过的: 编辑:

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);
        }));
};

部分实现:

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
}

问题是下载 URL 不是由函数 return 编辑的,我收到未处理的承诺拒绝:"can't find variable: imgUrl"

react-native-firebase 实现此目标有何帮助?

您实际上需要 return 来自 uploadImg 的值。现在 return 什么都没有。在 imageRef.putFile() 之前添加一个 return 关键字到 return 其链式承诺 returns.

的承诺