gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:48:18 25 4
gpt4 key购买 nike

Does not delete stored document in cloud-firestore.

当我运行我的程序时,会显示 5 个按钮(因为我的 firestore 文档是 5 个),当我单击按钮时会显示错误 test.html:1 Uncaught ReferenceError: ds5678fhiksnie is not defined 在 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>

最佳答案

使用 JavaScript 文字

 <html>
<head>

</head>
<body>
<div class="parent">

</div>
<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>
// Initialize Firebase
var config = {
apiKey: ####,
authDomain: ####,
databaseURL: ####,
projectId: ###,
storageBucket: ###,
messagingSenderId: ###
};
firebase.initializeApp(config);


</script>
<script>
var db = firebase.firestore();

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);
});


var p = document.querySelector(".parent");
db.collection("users").get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
var d=String(doc.id);
console.log(d);

p.innerHTML+=`<button onclick="deleti('${d}')">Ok</button>`;
});
});
function deleti(v) {
v=v.toString();

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>

关于javascript - 单击按钮时不会从 firebase 中删除文档(onclick 方法),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48459405/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com