firebase.storage()无法正常运行,但可以在Google的示例代码中运行

时间:2019-03-12 20:39:48

标签: javascript html firebase firebase-storage

我的JavaScript显示错误:

  

firebase.storage()不起作用

但这在Google代码实验室的示例代码中起作用。

let file;
        function handleFileUploadChange(e) {
                file = e.target.files[0];
                }

        function handleFileUploadSubmit(e) {
        console.log("handgle");

        firebase.firestore().collection('messages').add({
        name: "sanket",
        imageUrl: " ",
        }).then(function (messageRef) {
        // 2 - Upload the image to Cloud Storage.
        var filePath = firebase.auth().currentUser.uid + '/' + messageRef.id 
        + '/' + file.name;
        return firebase.storage().ref(filePath).put(file).then(function 
        (fileSnapshot) {
            // 3 - Generate a public URL for the file.
         return fileSnapshot.ref.getDownloadURL().then((url) => {
         // 4 - Update the chat message placeholder with the image's 

         return messageRef.update({
         imageUrl: url,
         storageUri: fileSnapshot.metadata.fullPath
             });
           });
         });
         }).catch(function (error) {
                 console.error('There was an error uploading a file to Cloud 
          Storage:', error);
         });

        }
        var change = document. getElementById('file-select');
        change. addEventListener('change', handleFileUploadChange);
        var sumbit = document. getElementById('file-submit');
        sumbit. addEventListener('click', handleFileUploadSubmit);
 <div id="filesubmit">
            <input type="file" id="file-select" accept="image/*"/>
            <input type="button" id="file-submit" value="SUBMIT">

     </div>

enter image description here

0 个答案:

没有答案