从Firebase获取图像URL

时间:2019-03-29 14:27:32

标签: javascript firebase firebase-storage

我正在将图像上传到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)

1 个答案:

答案 0 :(得分:2)

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

snapshot.ref.getDownloadURL().then(function(downloadUrl) {
    ...