【问题标题】:How to upload an image to firebase storage and then return the download url?如何将图像上传到 Firebase 存储,然后返回下载 url?
【发布时间】:2020-01-25 09:18:34
【问题描述】:

我正在尝试将图像上传到 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 可以帮助您实现这一目标吗?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 哦,可能是因为 getDownloadURL() 返回了一个承诺?
    • 是的,它确实返回了一个 Promise,但即使没有,您仍然需要让您的函数返回链中第一个 Promise 的结果。
    猜你喜欢
    • 2019-07-24
    • 2021-04-28
    • 2019-03-24
    • 1970-01-01
    • 1970-01-01
    • 2018-01-24
    • 2020-11-12
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多