javascript - Firebase error: Uncaught TypeError: ref.once is not a function -
i trying use firebase upload file enter file in data base @ same time. (i keep track of how many uploads there can rename file unique) uploaded, , put copy in database can not number of how many files in child folder using once() , getchildren() methods. keep getting error. doing wrong? count number error coming here:
var ref = firebase.database(); ref.once("value") .then(function(snapshot) { var numbersamples = snapshot.child(sampletype).numchildren(); //number of children });
full code:
<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 , check 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 uploader.classlist.remove('hide'); //unhide progress bar //create storage reference 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 problem uploading 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 , return children of sampletype console //dbreftype.on('value', snap => console.log(snap.val())); //create new reference file //var samplefileref = dbreftype.child("sample"); //todo: list how many in list //add new file list } ); } } </script>
nevermind, got it. used
dbreftype.once('value', function(snapshot) { alert('count: ' + snapshot.numchildren()); });
Comments
Post a Comment