单击按钮时不从firebase删除文档(onclick方法)

时间:2018-01-26 10:18:28

标签: javascript firebase google-cloud-firestore

  

不会在cloud-firestore中删除存储的文档。

当我运行我的程序时,显示5个按钮(因为我的firestore文档是5),当我单击按钮然后显示错误 test.html:1未捕获的ReferenceError:ds5678fhiksnie未定义     在HTMLButtonElement.onclick(test.html:1) 我想  单击按钮时,将删除基于文档的ID。

<html>
<body>
    <!-- parent named class inner buttons according document stored in cloud-firestore-->
    <div class="parent"></div>
    <-- api -->
    <script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>

    <script
        src="https://www.gstatic.com/firebasejs/4.9.0/firebase-firestore.js"></script>


    <script>
// web setup this is sample
        firebase.initializeApp({
         apiKey: '### FIREBASE API KEY ###',
         authDomain: '### FIREBASE AUTH DOMAIN ###',
         projectId: '### CLOUD FIRESTORE PROJECT ID ###'
});
firebase.initializeApp(config);
</script>
    <script>
        var db = firebase.firestore();
        // inserting one record automatic id
        db.collection("users").add({
            first: "Ada",
            last: "Lovelace",
            born: 2000
        })
        .then(function(docRef) {
            console.log("Document written with ID: ", docRef.id);
        })
        .catch(function(error) {
            console.error("Error adding document: ", error);
        });

        // get element
        var p =document.querySelector(".parent");
       // display the document
        db.collection("users").get().then(function(querySnapshot) {
            querySnapshot.forEach(function(doc) {
                var d=String(doc.id);
                console.log(d);
                // if stored documents are 5 then 5 buttons will display 
                p.innerHTML+="<button onclick=delet("+String(d)+")>Hello</button>";
            });
        });

         // delete function(id passing)
        function delet(v) {
        console.log(v);
        db.collection("users").doc(v).delete().then(function() {
            console.log("Document successfully deleted!");
        }).catch(function(error) {
            console.error("Error removing document: ", error);
        });
        }

        </script>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

使用JavaScript文字

Background

答案 1 :(得分:0)

尝试collection.child(v).remove()然后处理你的回调。