首页 > 解决方案 > Remove() 函数在 web 的 firebase 中不起作用

问题描述

这就是我的 firebase 数据库的样子 这是.js文件。我试图做的是在单击 html 页面中的特定行时创建一个 onclick 删除功能。到目前为止,我可以获得子键(嵌套),但删除函数会以某种方式抛出错误,即 career-delete.html:1 Uncaught ReferenceError: MCu9V4ypS is not defined at HTMLButtonElement.onclick (career-delete.html:1)。

 userImagesRef1.once("value", function(snapshot) {
      var val1, val2, val3;
      var ParentKey = snapshot.key;
    console.log("PK"+ParentKey);
      snapshot.forEach(function(childSnapshot) {
          childSnapshot.forEach(function(snap){
              var childKey = snap.key;
              console.log("CK" + childKey);
          
          var Vacancy = snap.child("VacancyNumber").val();
          console.log("VacancyNumber" + Vacancy);

        //   var NoticeNumber = snap.child("Service").val();
        //   var NameofWork = snap.child("Title").val();
                snap.child('pictures').forEach(function(openPicturesSnap){
                    console.log("KEY LINKS: " + openPicturesSnap.key);
                    // var i = 0;
                    if(openPicturesSnap.key == 0){
                        val1 = openPicturesSnap.val();
                        // console.log("LINKS1111::::" + val1 );
                    }
                    if(openPicturesSnap.key == 1){
                        val2 = openPicturesSnap.val();
                        
                    }
                    if(openPicturesSnap.key == 3){
                        val1 = openPicturesSnap.val();
                        
                    }
                })   
                var Service = String(snap.child("Service").val());
                // console.log(Service)
                var ts = snap.child("timestamp").val();
                // console.log("TS:" + ts);
                var Title = String(snap.child("Title").val());
                // console.log(Title)
                let Numberofposts = String(snap.child("NumberofPosts").val());
                // console.log(Numberofposts);
                var SNo = "";
                var State = snap.child("Status").val();
                var IssueDate = snap.child("IssueDate").val();
                var ClosingDate = snap.child("ClosingDate").val();
                var Remarks = String(snap.child("Remarks").val());
        

                $("#tableBody").append("<tr><td>"+ SNo +"</td><td><a href = '" + val1+ "'>" +Vacancy+"</a></td>"+ "<td><a href = '" +val2 + "'>" +Service+"</a></td><td>" + Title + "</td><td>"+Numberofposts+"</td><td>" + IssueDate + "</td><td>"+ClosingDate + "</td> <td>" + State+"<td><a href = '" +val3 + "'>" +Remarks+"</a>"+ "</td><td>"+'<button type="button"  onClick=Delete('+childKey+'); class="btn  delete">X</button>'+"</td></tr>" );        
              })
            }) 
              function Delete(key){
                      var feedRef = firebase.database().ref("user-images").child(key);
                      feedRef.remove()
                      .then(function(){
                          console.log("Remove succeeded.")
                          alert("Added");
                          // console.log(key.val());
                      })
                      .catch(function(error){
                          console.log("Remove Failed!"+error.message)
                      });
              } 
          });

标签: javascriptfirebasefirebase-realtime-database

解决方案


您需要添加一些双引号,如下所示:

<button type="button"  onclick="Delete('" +childKey+ "');" class="btn  delete">X</button>

推荐阅读