如何使用Javascript在Firebase中执行特定子项的删除操作

时间:2019-02-03 16:21:46

标签: javascript html firebase firebase-realtime-database

我正在尝试为使用Firebase Realtime db的表编写删除功能。我对此有些陌生,但我需要帮助。

到目前为止,我正在尝试获取子ID并将其传递给函数以将其删除。

    var mainApp = {};
    (function(){

    var firebase = app_firebase;
    var uid = null;


    function fnHRead() {
    var path = 'institutions/' + uid;
    app_firebase.databaseApi.read(path, sucessFn, messageHandler)
    function sucessFn(snapShot){
        if(!!snapShot && !!snapShot.val()){
        //console.log(snapShot.val());
        var inst_info = snapShot.val();
        var keys = Object.keys(inst_info);
        console.log(keys);
        var html = "<table class='centered' border='1|1'>";
        html+= "<thead>";
        html+= "<tr>";
        html+= "<th>Institution Name</th>";
        html+= "<th>Proprietor</th>";
        html+= "<th>Phone</th>";
        html+= "<th>Email</th>";
        html+= "<th>Locality</th>";
        html+= "<th>City</th>";
        html+= "<th>Manage</th>";
        html+= "</tr>";
        html+= "</thead>";

        for (var i = 0;i < keys.length; i++){
            var k = keys[i];
            var name = inst_info[k].inst_name;
            var ownerName = inst_info[k].inst_owner;
            var email = inst_info[k].inst_email;
            var phone = inst_info[k].inst_phone;
            var locality = inst_info[k].inst_locality;
            var city = inst_info[k].inst_city;
            var path = keys[i];

            html+="<tr>";
            html+="<td>"+name+"</td>";
            html+="<td>"+ownerName+"</td>";
            html+="<td>"+phone+"</td>";
            html+="<td>"+email+"</td>";
            html+="<td>"+locality+"</td>";
            html+="<td>"+city+"</td>";
            html+="<td><a class='waves-effect waves-light btn red' onclick='mainApp.deletePatient("+path+")'>Delete</a></td>";
            html+="</tr>";
            //console.log(name,ownerName,email,phone,locality,city);
            console.log(path);

        }
        html+="</table>";
            document.getElementById("manageExist").innerHTML = html;
            M.toast({html: 'Institution list Fetch Sucessfull', classes: 'rounded'});

        }else{
        console.log("No data");
        M.toast({html: 'Institution list Fetch Unsucessful', classes: 'rounded'});
        }
    }   
    }

    function fnHDelete(path){
        app_firebase.databaseApi.delete(path, messageHandler);

    }
    mainApp.deletePatient = fnHDelete;
    })()

另一个.js包含Firebase凭据和CRUD操作功能

    function dbDelete(path, callBack){
        if(!path) return;
        app_firebase.database().ref(path).remove(callBack);

    }

我进入控制台错误:

    dashboard.html:1 Uncaught ReferenceError: LXntMDT74LL_suY6Ar0 is not defined
    at HTMLAnchorElement.onclick (dashboard.html:1)

1 个答案:

答案 0 :(得分:0)

首先,javascript是一种专门用于客户端的语言,由于多种安全问题,用户应用程序在任何情况下都不应直接与数据库对话。

要回答您的问题:Firebase有一个removeValue()命令,您可以用来删除元素:Docs Link

编辑:您正在调用要删除的回调,而不是路径。不太确定,这是否是错误,但是值得一试。