gpt4 book ai didi

javascript - Firebase 错误 : Uncaught TypeError: ref. once is not a function

转载 作者:行者123 更新时间:2023-11-30 15:50:15 29 4
gpt4 key购买 nike

我正在尝试使用 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>

最佳答案

没关系,我明白了。用过

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

关于javascript - Firebase 错误 : Uncaught TypeError: ref. once is not a function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39459744/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com