Javascript Firebase web文件上载代码:“应用程序/重复应用程序”

Javascript Firebase web文件上载代码:“应用程序/重复应用程序”,javascript,jquery,firebase,firebase-storage,Javascript,Jquery,Firebase,Firebase Storage,我试图通过firebase在我的网站上上传图像和视频, 第一次上传有效,第二次上传在我的控制台上显示此消息: firebase.js:1未捕获 {代码:app/duplicate app,消息:Firebase:Firebase app name'[DEFAULT]'已存在app/duplicate app.,name:[DEFAULT],stack:[DEFAULT]:Firebase:Firebase app name'[DEFAULT]…} 代码:应用程序/重复应用程序 消息:Fireba

我试图通过firebase在我的网站上上传图像和视频, 第一次上传有效,第二次上传在我的控制台上显示此消息:

firebase.js:1未捕获 {代码:app/duplicate app,消息:Firebase:Firebase app name'[DEFAULT]'已存在app/duplicate app.,name:[DEFAULT],stack:[DEFAULT]:Firebase:Firebase app name'[DEFAULT]…} 代码:应用程序/重复应用程序 消息:Firebase:Firebase应用程序名为“[默认]”已存在应用程序/重复应用程序。 名称:[默认值] 堆栈:[DEFAULT]:Firebase:Firebase应用程序名为“[DEFAULT]”已存在应用程序/重复应用程序。↵ 错误地↵ 在Object.initializeApp↵ 在文件输入↵ 在兰开夏。↵ 在HTMLAnchorElement.DISPATH↵ 在htmlanchorement.elemData.handle 原型:错误

此外,当我尝试上传视频时,它会返回一条消息,表示我没有发送任何数据:

未捕获e{code\:storage/invalid argument,message\:Firebase storage:invalid argument放入索引0:应为Blob或File.,serverResponse\:null,name\:FirebaseError}

HTML:

JS文件

 $(document).ready(function(){
   var config = {
    apiKey: "",
    authDomain: "web-test-.com",
    databaseURL: "com",
    projectId: ",
    storageBucket: ".appspot.com",
    messagingSenderId: ""
  };
    firebase.initializeApp(config);
});

    $(function(){
        $("#postBtn").on('click', function(e){
            e.preventDefault();
            var postField = $('#postField').val();
            var image = $('#imgupload')[0].files[0];
            var video = $('.videoUpload')[0].files[0];
            fileInput(postField,image,video);
                $('#contentToUpload').hide();
                $('.videoUploadIcon').show();       
        });
    });
// <!-- ADD POST -->
// INPUT FILE FUNCTION
function fileInput(postField, image, video) {

    if (image != null) {
        uploadFile(image)
    } else if (video != null) {
        uploadFile(video)
    } else {
        console.log('youre upload cant be blank!');
    }


}


function uploadFile(file) {
var database = firebase.database();
var storage = firebase.storage();
const storageRef = storage.ref();
    var generatedName = Math.random().toString(36).substr(2, 20);
    const uploadTask = storageRef.child(`images/${generatedName}`).put(file); //create a child directory called images, and place the file inside this directory
    uploadTask.on('state_changed', (snapshot) => {
        uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, function(snapshot) {
            var percent = snapshot.bytesTransferred / snapshot.totalBytes * 100;
            $('.progress').css("display", "block");
            $('.numPercentage').css("display", "block");
            $('.determinate').width(parseInt(percent) + '%');
            $('.numPercentage').html(parseInt(percent) + '%');
        });
    }, (error) => {
        // Handle unsuccessful uploads
        console.log(error);
    }, () => {
        uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
            console.log('File available at', downloadURL);
        });
        console.log('success');
        setTimeout(function() {
            $('.numPercentage').fadeOut(1000).css("display", "none");
            $('.determinate').fadeOut(1000).css("display", "none");
            $('.progress').fadeOut(1000).css("display", "none");
            $('.numPercentage').val("");
            $('.determinate').val("");
        }, 2000);
        document.getElementById("postField").value = null;
        document.getElementById("imgupload").value = null;
        document.getElementById("videoUpload").value = null;
        setTimeout(function() {
            $('.feed-posts').load('/php/posts.php').fadeIn('slow');
        }, 1000);
    });
}
}

// INPUT FILE FUNCTION

每次页面加载只能调用firebase.initializeApp一次。现在,您每次上传都会调用它。

我已经删除了firebase配置,因为我没有使用任何规则。同样的事情发生了对不起,我的错误,您是对的,我没有调用firebase.initializeApp,谢谢您现在它可以正常工作了
 $(document).ready(function(){
   var config = {
    apiKey: "",
    authDomain: "web-test-.com",
    databaseURL: "com",
    projectId: ",
    storageBucket: ".appspot.com",
    messagingSenderId: ""
  };
    firebase.initializeApp(config);
});

    $(function(){
        $("#postBtn").on('click', function(e){
            e.preventDefault();
            var postField = $('#postField').val();
            var image = $('#imgupload')[0].files[0];
            var video = $('.videoUpload')[0].files[0];
            fileInput(postField,image,video);
                $('#contentToUpload').hide();
                $('.videoUploadIcon').show();       
        });
    });
// <!-- ADD POST -->
// INPUT FILE FUNCTION
function fileInput(postField, image, video) {

    if (image != null) {
        uploadFile(image)
    } else if (video != null) {
        uploadFile(video)
    } else {
        console.log('youre upload cant be blank!');
    }


}


function uploadFile(file) {
var database = firebase.database();
var storage = firebase.storage();
const storageRef = storage.ref();
    var generatedName = Math.random().toString(36).substr(2, 20);
    const uploadTask = storageRef.child(`images/${generatedName}`).put(file); //create a child directory called images, and place the file inside this directory
    uploadTask.on('state_changed', (snapshot) => {
        uploadTask.on(firebase.storage.TaskEvent.STATE_CHANGED, function(snapshot) {
            var percent = snapshot.bytesTransferred / snapshot.totalBytes * 100;
            $('.progress').css("display", "block");
            $('.numPercentage').css("display", "block");
            $('.determinate').width(parseInt(percent) + '%');
            $('.numPercentage').html(parseInt(percent) + '%');
        });
    }, (error) => {
        // Handle unsuccessful uploads
        console.log(error);
    }, () => {
        uploadTask.snapshot.ref.getDownloadURL().then(function(downloadURL) {
            console.log('File available at', downloadURL);
        });
        console.log('success');
        setTimeout(function() {
            $('.numPercentage').fadeOut(1000).css("display", "none");
            $('.determinate').fadeOut(1000).css("display", "none");
            $('.progress').fadeOut(1000).css("display", "none");
            $('.numPercentage').val("");
            $('.determinate').val("");
        }, 2000);
        document.getElementById("postField").value = null;
        document.getElementById("imgupload").value = null;
        document.getElementById("videoUpload").value = null;
        setTimeout(function() {
            $('.feed-posts').load('/php/posts.php').fadeIn('slow');
        }, 1000);
    });
}
}

// INPUT FILE FUNCTION