亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用按鈕單擊事件時,Firestore 不會添加新記錄(網絡)

使用按鈕單擊事件時,Firestore 不會添加新記錄(網絡)

瀟湘沐 2023-04-01 15:14:40
我是 Firebase Firestore 的新用戶。我有一個集合(“詞匯”),我想添加新文檔(“單詞”、“含義”)。該代碼有效并添加了新記錄。但是當我使用相同的代碼在單擊按鈕后執行時,它不起作用并且沒有錯誤代碼。當我在 click 函數下編寫 Alert("test") 以測試是否被調用時,我看到“test”消息沒有問題。我認為,它表明監聽器在點擊時被調用。但我認為 add() 部分沒有被調用。可能的原因是什么?此代碼有效:      database.collection("vocabulary").add({        word:"Book",        meaning:"xxx",      })      .then(function() {        console.log("Document successfully written!");      })      .catch(function(error) {        console.error("Error writing document: ", error);      });但相同的代碼在按鈕單擊下不起作用:<button id="savebutton">Insert Record</button>......const save=document.querySelector("#savebutton");  save.addEventListener("click",function() {       database.collection("vocabulary").add({        word:"Book",        meaning:"xxx",      })      .then(function() {        console.log("Document successfully wriiten!");      })      .catch(function(error) {        console.error("Error writing document: ", error);      });    }); 
查看完整描述

1 回答

?
jeck貓

TA貢獻1909條經驗 獲得超7個贊

OP 通過添加以下代碼解決了該問題:


x.preventDefault();

從我收集的內容來看,更新后的代碼看起來像這樣:


<button id="savebutton">Insert Record</button>

? ? ......


const save=document.querySelector("#savebutton");

? save.addEventListener("click",function(x) {

? x.preventDefault();?

? ? ? database.collection("vocabulary").add({

? ? ? ? word:"Book",

? ? ? ? meaning:"xxx",

? ? ? })

? ? ? .then(function() {

? ? ? ? console.log("Document successfully wriiten!");

? ? ? })

? ? ? .catch(function(error) {

? ? ? ? console.error("Error writing document: ", error);

? ? ? });

? ? });

因此,根據公共文檔,OP 阻止了“x”的默認操作,解決了這個問題。



查看完整回答
反對 回復 2023-04-01
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號