未捕获(承诺)错误:Reference.set失败:第一个参数包含未定义的属性'kategoriler.Nature.thumbnail'
当我遇到以下错误时,您认为问题出在哪里?我通过观看新视频来做到这一点,但我也没有工作。我还通过firebase创建了数据库,并且将权限设置为true。请帮我。预先非常感谢。
错误控制台
$("#save-category").click(function(){
$("#category-name").removeClass("is-invalid");
$("#category-desc").removeClass("is-invalid");
$("#category-thumbnail").removeClass("is-invalid");
var catname = $("#category-name").val();
var desc = $("#category-desc").val();
var thumbnail = $("#category-thumbnail").prop("files")[0];
if(!catname){
$("#category-name").addClass("is-invalid");
return;
}
if(!desc){
$("#category-desc").addClass("is-invalid");
return;
}
if(thumbnail == null){
$("#category-thumbnail").addClass("is-invalid");
return;
}
if($.inArray(thumbnail["type"], validImageTypes)<0){
$("#category-thumbnail").addClass("is-invalid");
return;
}
var database = firebase.database().ref("kategoriler/"+catname);
database.once("value").then(function(snapshot){
if(snapshot.exists()){
$("#result").attr("class", "alert alert-danger");
$("#result").html("Bu kategori zaten var");
} else {
var name = thumbnail["name"];
var ext = name.substring(name.lastIndexOf("."), name.length);
var thumbname = new Date().getTime();
var storageRef = firebase.storage().ref(catname + "/" + thumbname + ext);
var uploadTask = storageRef.put(thumbnail);
uploadTask.on("state_changed",
function progress(snapshot){
var percentage = (snapshot.bytesTransferred / snapshot.totalBytes) * 100;
$("#upload-progress").html("%" + percentage);
$("#upload-progress").attr("style", "width:"+percentage + "%");
},
function error(err){
},
function complete(){
var thumbnailUrl = uploadTask.snapshot.downloadURL;
var cat = {
"thumbnail": thumbnailUrl,
"desc": desc
};
database.set(cat, function(err){
if(err){
$("#result").attr("class", "alert alert-danger");
$("#result").html(err.message);
} else {
$("#result").attr("class", "alert alert-success");
$("#result").html("Kategori eklendi");
}
});
}
);
}
});
});
答案 0 :(得分:1)
您的问题在于“功能完成”中的网址不能未定义
在Firebase文档中查看他们的工作方式:
function() {
// Handle successful uploads on complete
// For instance, get the download URL: https://firebasestorage.googleapis.com/...
uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
console.log('File available at', downloadURL);
});
这是页面的链接:https://firebase.google.com/docs/storage/web/upload-files
答案 1 :(得分:0)
在您的函数complete()中,直接使用storageRef来获取下载URL storageRef.getDownloadURL()。然后,(function(url)将在参数url中给出downloadURL。
function complete(){
storageRef.getDownloadURL().then(function(url){
console.log(url);
var cat={
"thumbnail":url,
"desc":desc
};
database.set(cat,function(err){
if(err){
$("#result").attr("class","alert alert-danger");
$("#result").html("Category already exist");
}
else{
$("#result").attr("class","alert alert-success");
$("#result").html("Category added");
}
});
});
}