从Firebase获取图片网址

问题描述 投票:0回答:1

我正在将图像上传到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)
javascript firebase firebase-storage
1个回答
2
投票

snapshot变量是UploadTaskSnapshot类型,没有storageRef孩子。你可能正在寻找snapshot.ref

snapshot.ref.getDownloadURL().then(function(downloadUrl) {
    ...
© www.soinside.com 2019 - 2024. All rights reserved.