繁体   English   中英

Firebase 存储错误 - 不是函数

[英]Firebase storage error - is not a function

使用html/JS ,我正在尝试编写一个文件。

我在html head 中包含了这个:

 <head>
          <script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-app.js"></script>
          <script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-firestore.js"></script>

然后在body的末尾我有:

//...
  <script src="javascripts/main.js"></script>

     <script>
              var config = {
                apiKey: "xxx",
                authDomain: "xxxx.firebaseapp.com",
                databaseURL: "https://xxxx.firebaseio.com",
                projectId: "xxxx",
                storageBucket: "xxxx.appspot.com",
                messagingSenderId: "xxxxx"
              };
              firebase.initializeApp(config);
              const db = firebase.firestore();
              db.settings({timestampsInSnapshots:true});
         </script>

然后在main.js文件上,当我尝试写入main.js时:

   var storageRef = firebase.storage().storage.ref()('me/' + file.name);
           var task=storageRef.put(file);
            task.on('state_changed',
                    function progress(snapshot){console.log(snapshot.bytesTransferred)},
                    function error(err){},
                    function complete(){});

我在控制台上收到此错误:

firebase.storage is not a function

编辑:

Tried again and got same error with :

        // Create a root reference
        var storageRef = firebase.storage().ref();
        // Create a reference to 'images/mountains.jpg'
        var finalRef = storageRef.child('me/' + file.name);  


                  finalRef.put(file).then(function(snapshot){
                      console.log('uploading');
                  }); 

你还需要添加这个:

<script src="https://www.gstatic.com/firebasejs/5.7.0/firebase-storage.js"></script>

能够使用firebase存储api。

也改变这个:

   var storageRef = firebase.storage().storage.ref()('me/' + file.name);

进入这个:

   var storageRef = firebase.storage().ref('me/' + file.name);

检查这个以获取更多信息:

https://firebase.google.com/docs/storage/web/create-reference

像这样改变你的脚本 main.js 位置

         <script>
                  var config = {
                    apiKey: "xxx",
                    authDomain: "xxxx.firebaseapp.com",
                    databaseURL: "https://xxxx.firebaseio.com",
                    projectId: "xxxx",
                    storageBucket: "xxxx.appspot.com",
                    messagingSenderId: "xxxxx"
                  };
                  firebase.initializeApp(config);
                  const db = firebase.firestore();
                  db.settings({timestampsInSnapshots:true});
             </script>


     //to here
     <script src="javascripts/main.js"></script>

检查您的 firebase 导入是否正确。 如果您错误地导入它,它将不起作用。

应该是这样的

import * as firebase from "firebase/app";

不是这个

import * as firebase from "firebase";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM