简体   繁体   English

如何按文件ID删除行

[英]How to delete row by document ID

I am able to add data no problem, but i cannot seem to figure out how to use remove properly... 我能够添加数据没问题,但是我似乎无法弄清楚如何正确使用删除...

I want the ability to remove records in the DB by each documents unique ID that gets autogenerated. 我希望能够通过自动生成的每个文档唯一ID来删除数据库中的记录。 ex: /lab-results/292AU3VhfMXOzieHtmiD 例如:/ lab-results / 292AU3VhfMXOzieHtmiD

What am i doing wrong here? 我在这里做错了什么? I keep getting "db.ref is not a function" when i try to issue the remove. 当我尝试发出remove时,我不断收到“ db.ref不是一个函数”。

Please see code for my attempts at getting this to work. 请查看代码以了解我如何使其正常工作。

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)


/////////

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM