代码之家  ›  专栏  ›  技术社区  ›  Curnelious

Firebase存储错误-不是一个函数

  •  0
  • Curnelious  · 技术社区  · 6 年前

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

    main.js 尝试写入storgae时的文件:

       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');
                      }); 
    
    2 回复  |  直到 6 年前
        1
  •  3
  •   Peter Haddad    6 年前

    您还需要添加以下内容:

    <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
  •  1
  •   Mars.Tsai    6 年前

    像这样更改脚本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>
    
        3
  •  0
  •   nipun-kavishka    3 年前

    import * as firebase from "firebase/app";
    

    不是这个

    import * as firebase from "firebase";