React Native: 从Firestore获取图像URL

huangapple go评论63阅读模式
英文:

React Native: Retrieve image URL from firestore

问题

这是一个将图像上传到Firebase存储并使用'getDownloadURL'函数检索URL的函数。

上传图像的部分正常工作,但在尝试访问URL时无法检索URL,因为它是在图像仍在上传时尝试访问URL。

请解决这个问题!!

const getGSTURI = async () => {
    if (GSTLoading) {
        return;
    }
    setGSTLoading(true);

    const result = await DocumentPicker.getDocumentAsync({
        copyToCacheDirectory: true,
    });
    console.warn(result);

    setGSTName(result.name);
    setGSTURI(result.uri);
    setGSTLoading(false);

    async function uploadGST(uri, name) {
        const blob = await new Promise((resolve, reject) => {
            const xhr = new XMLHttpRequest();
            xhr.onload = function () {
                resolve(xhr.response);
            };
            xhr.onerror = function (e) {
                console.warn(e);
                reject(new TypeError("Network request failed"));
            };
            xhr.responseType = "blob";
            xhr.open("GET", uri, true);
            xhr.send(null);
        });

        const storageRef = ref(storage, `sellers/${sellerID}/${name}`);

        uploadBytes(storageRef, blob).then(() => {
            console.warn("GST Upload Successfull");
        });

        getDownloadURL(ref(storage, `sellers/${sellerID}/${name}`))
            .then((url) => {
                // 'url' 是存储在firestorage中的 'images' 的下载URL
                console.log(url);
                setGSTURL(url);
                console.log(GSTURL);
            })
            .catch((error) => {
                "下载时出现错误";
            });
        // 我们已经处理完blob,关闭并释放它
        blob.close();
    }

    uploadGST(GSTURI, GSTName);
};
英文:

This is a function that uploads an image to the firebase storage and then retrieves the URL using the 'getDownloadURL' function.

The uploading of images works fine but it fails to retrieve the URL as it is trying to access the URL while the image is still uploading.

Please solve this problem !!

const getGSTURI = async () => {
if (GSTLoading) {
return;
}
setGSTLoading(true);
const result = await DocumentPicker.getDocumentAsync({
copyToCacheDirectory: true,
});
console.warn(result);
setGSTName(result.name);
setGSTURI(result.uri);
setGSTLoading(false);
async function uploadGST(uri, name) {
const blob = await new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.onload = function () {
resolve(xhr.response);
};
xhr.onerror = function (e) {
console.warn(e);
reject(new TypeError("Network request failed"));
};
xhr.responseType = "blob";
xhr.open("GET", uri, true);
xhr.send(null);
});
const storageRef = ref(storage, `sellers/${sellerID}/${name}`);
uploadBytes(storageRef, blob).then(() => {
console.warn("GST Upload Successfull");
});
getDownloadURL(ref(storage, `sellers/${sellerID}/${name}`))
.then((url) => {
// `url` is the download URL for 'images ' stored in firestorage
console.log(url);
setGSTURL(url);
console.log(GSTURL);
})
.catch((error) => {
"Errors while downloading";
});
// We're done with the blob, close and release it
blob.close();
}
uploadGST(GSTURI, GSTName);
};

答案1

得分: 1

你必须等待uploadBytes函数完成后才能尝试检索URL。

而不是:

uploadBytes(storageRef, blob).then(() => {
  console.warn("GST Upload Successfull");
});

你可以使用如下的await操作符等待任务完成:

try {
  await uploadBytes(storageRef, blob);
  console.warn('GST Upload Successfull');
} catch (e) {
  console.warn('GST Upload Failed', e);
}
英文:

you have to wait for the uploadBytes function to complete before trying to retrieve the url

instead of

uploadBytes(storageRef, blob).then(() => {
console.warn("GST Upload Successfull");
});

you can use the await operator as below to wait for the task to complete

try {
await uploadBytes(storageRef, blob);
console.warn('GST Upload Successfull');
} catch (e) {
console.warn('GST Upload Failed', e);
}

huangapple
  • 本文由 发表于 2023年2月18日 03:12:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488392.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定