Firebase存储错误-不是功能

时间:2018-12-13 07:23:46

标签: javascript firebase google-cloud-firestore

我正在使用html/JS来写文件。

我将此放在html头上了:

 <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>

然后在我尝试写入storgae的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');
                  }); 

3 个答案:

答案 0 :(得分:1)

像这样更改脚本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>

答案 1 :(得分:1)

您还需要添加此内容:

<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

答案 2 :(得分:0)

检查您的 firebase 导入是否正确。如果您导入错误,它将无法正常工作。

应该是这样

import * as firebase from "firebase/app";

不是这个

import * as firebase from "firebase";