如何将图片上传到Firebase存储,然后返回下载网址?

时间:2020-01-25 00:54:27

标签: javascript google-cloud-storage react-native-firebase

我正在尝试将图像上传到react-native项目中的Firebase存储中,然后在上传完成后返回下载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,我未处理的承诺被拒绝:“找不到变量:imgUrl”

通过react-native-firebase实现这一目标有帮助吗?

1 个答案:

答案 0 :(得分:3)

您实际上需要从uploadImg返回一个值。现在它什么也没返回。在return之前添加imageRef.putFile()关键字,以返回其链式承诺返回的承诺。

相关问题