开发者问题收集

从 Firebase 获取图片 URL

2019-03-29
1233

我正在将图像上传到 firebase,然后在上传完成后尝试检索 URL,但出现此错误。

[Error] Error uploading image:  – TypeError: undefined is not an object (evaluating 'snapshot.storageRef.getDownloadURL') — speakers.js:150
TypeError: undefined is not an object (evaluating 'snapshot.storageRef.getDownloadURL') — speakers.js:150
    (anonymous function) (speakers.js:161)
    promiseReactionJob

图像上传正常,但我没有获取 URL。

var storageRef = firebase.storage().ref();

function uploadPhoto() {
    if (selectedFile != null) {
        var filePath = "Speaker Images/" + selectedFile.name + Date.now();
        var upload = storageRef.child(filePath).put(selectedFile).then(function(snapshot) {
            snapshot.storageRef.getDownloadURL().then(function(downloadUrl) {
                console.log(downloadUrl);
                uploadImageUrl = downloadUrl;
                if (docId != null) {
                    updateSpeaker();
                } else {
                    createNewSpeaker();
                }
            });
        })
        .catch(function(error) {
            console.error("Error uploading image: ", error);
        });
    } else {
        if (docId != null) {
            updateSpeaker();
        } else {
            createNewSpeaker();
        }
    }
}

快照似乎没有问题。

[Log] Ee {bytesTransferred: 30255, totalBytes: 30255, state: "success", metadata: Object, task: e, …} (speakers.js, line 150)
2个回答

snapshot 变量的类型为 UploadTaskSnapshot ,没有 storageRef 子项。您可能正在寻找 snapshot.ref

snapshot.ref.getDownloadURL().then(function(downloadUrl) {
    ...
Frank van Puffelen
2019-03-29

使用 Reference.getDownloadURL

const url = await storage().ref('images/profile-1.png').getDownloadURL();

https://rnfirebase.io/storage/usage

Jogy Felix
2021-09-22