如何按文件ID删除行

时间:2019-08-03 11:25:56

标签: javascript firebase google-cloud-firestore

我能够添加数据没有问题,但是我似乎无法弄清楚如何正确使用删除...

我希望能够通过自动生成的每个文档唯一ID来删除数据库中的记录。例如:/ lab-results / 292AU3VhfMXOzieHtmiD

我在这里做错了什么?当我尝试执行删除操作时,我不断收到“ db.ref不是函数”的消息。

请参见代码,以了解我如何使其正常工作。

HTML CODE

<script src="https://www.gstatic.com/firebasejs/6.1.1/firebase-app.js"></script> 
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://www.gstatic.com/firebasejs/6.1.1/firebase-firestore.js"></script>
<script src="https://www.gstatic.com/firebasejs/6.1.1/firebase-auth.js"></script>
<!-- TODO: Add SDKs for Firebase products that you want to use
     https://firebase.google.com/docs/web/setup#config-web-app -->


<script>
        // Your web app's Firebase configuration
        var firebaseConfig = {
            apiKey: "",
            authDomain: ".firebaseapp.com",
            databaseURL: "https://.firebaseio.com",
            projectId: "",
            storageBucket: ".appspot.com",
            messagingSenderId: "",
            appId: ""
        };
        // Initialize Firebase
        firebase.initializeApp(firebaseConfig);
        const db = firebase.firestore();
        const auth = firebase.auth();

</script>
JS FILE CODE


//Add to firebase db THIS WORKS FINE

var el = document.getElementById('test');
if(el){
labTestForm.addEventListener('submit', (e) =>{
    e.preventDefault();
    db.collection('tests').add({
        labTestID: labTestForm.labTestID.value,
        labTestCode: labTestForm.labTestCode.value,
        labTestName: labTestForm.labTestName.value,

    })
    //Reset for fields after submit
    labTestForm.reset();


})

}

///////////////////
//TEST JS - THROWS ERROR IN CONSOLE "db.ref is not a function"


var labResultsFB = db.ref("lab-results/0nH95TRY5xwnPJovwEFd")
labResultsFB.remove();

console.log('remove fired for:' + key)


/////////

1 个答案:

答案 0 :(得分:0)

var labResultsFB = db.doc("lab-results/0nH95TRY5xwnPJovwEFd")
labResultsFB.delete();