Firebase错误:未捕获TypeError:ref.once不是函数

时间:2016-09-12 22:33:57

标签: javascript html angularjs firebase firebase-realtime-database

我正在尝试使用firebase上传文件,同时在我的数据库中输入文件。 (我想跟踪有多少上传,所以我可以将文件重命名为独特的东西)我成功上传了,并且还在数据库中放了一个副本,但我无法得到一个数字,其中有多少文件在使用once()和getChildren()方法的子文件夹。我一直在收到错误。我究竟做错了什么?计数错误来自此处:

var ref = firebase.database();
                          ref.once("value")
                            .then(function(snapshot) {
                              var numberSamples = snapshot.child(sampleType).numChildren(); //number of children
                            });

完整代码:

<script src="https://www.gstatic.com/firebasejs/3.3.2/firebase.js"></script>
            <script>
              // Initialize Firebase
              var config = {
                apiKey: "AIzaSyC9jUMxOvvdgcBvXecvpRiactG6ffbwTgg",
                authDomain: "instasample-d8eea.firebaseapp.com",
                databaseURL: "https://instasample-d8eea.firebaseio.com",
                storageBucket: "instasample-d8eea.appspot.com",
              };
              firebase.initializeApp(config);

              //remove error on click
              function removeSampleClass() {
                $("#sampleType").removeClass('sampleTypeError')
                $("#sampleType").addClass('sampleType')
              }

              function removeFileClass() {
                $("#fileButton").removeClass('fileButtonError')
                $("#fileButton").addClass('fileButton')
              }


              // upload files and check for errors
              function uploadFile(){   //on GO
                  //get elements
                  var uploader = document.getElementById('uploader')
                  var fileButton = document.getElementById('fileButton')
                  //var file = fileButton.target.files[0]; //get file
                  var sampleType = document.getElementById('sampleType').options[document.getElementById('sampleType').selectedIndex].text;  //get folder location
                  var file = fileButton.files[0]; //get file

                  if (sampleType == "Select Type" && fileButton.value == "") {  //check both selection
                    $("#sampleType").addClass('sampleTypeError')
                    $("#sampleType").removeClass('sampleType')
                    $("#fileButton").addClass('fileButtonError')
                    $("#fileButton").removeClass('fileButton')
                  }
                  else if (fileButton.value == "") {                            //check file selection
                    $("#fileButton").addClass('fileButtonError')
                    $("#fileButton").removeClass('fileButton')
                  }
                  else if (sampleType == "Select Type") {                       //check sample selection
                   $("#sampleType").addClass('sampleTypeError')
                    $("#sampleType").removeClass('sampleType')
                  }
                  else{    //upload if good
                  uploader.classList.remove('hide');  //unhide progress bar
                  //create storage reference with sampleType 
                  var storageRef = firebase.storage().ref("/" + sampleType + "/" + file.name);


                  //upload file
                  var task = storageRef.put(file);

                  //update progress bar
                  task.on('state_changed',
                    function progress(snapshot){
                        var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
                        uploader.value = percentage;
                    },
                    function error() {
                      alert('There was a problem uploading your file')
                    },
                    function complete() {
                      //get root reference
                      const dbRefType = firebase.database().ref().child(sampleType);

                      var ref = firebase.database();
                          ref.once("value")
                            .then(function(snapshot) {
                              var numberSamples = snapshot.child(sampleType).numChildren(); // 2 ("first", "last")
                            });


                      var updates = {};
                        updates["/" + sampleType + "/" + sampleType + "Sample" + numberSamples ] = numberSamples;
                        return firebase.database().ref().update(updates);
                      //sync data and return children of sampleType to console
                      //dbRefType.on('value', snap => console.log(snap.val()));
                      //create new reference to file
                      //var sampleFileRef = dbRefType.child("sample");


                      //TODO: list how many are in list 
                      //add new file to list


                    }
                  );
                }
              }


            </script>

1 个答案:

答案 0 :(得分:2)

没关系,我明白了。用过

dbRefType.once('value', function(snapshot) { alert('Count: ' + snapshot.numChildren()); });