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

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

如何處理 catch 函數?

如何處理 catch 函數?

波斯汪 2024-01-22 15:41:06
我在cloud firestor中創建了一個數據庫,所以現在我想通過輸入字段向其中添加一些信息,當輸入字段為空信息時存儲問題并且catch函數不起作用,我該如何修復它,并進行catch功能工作。這是代碼:HTML:<div class="container">           <input  type="text" placeholder="Enter the country" id="country-field"  required>          <p class="selectPar">Enter City</p>          <input type="text" placeholder="Enter the city" id="city-field" required>          <p class="selectPar">Enter the place name</p>          <input type="text" placeholder="Enter the place name" id="placename-field" required>          <p class="selectPar">Enter Address</p>          <input type="text" placeholder="Enter the address" id="address-field" required>          </div>        </div>        <div id="btnFrmAlignSec">          <button id="btnFrm" type="button" onclick="AddToDataBase()">Send to admin</button>        </div>JavaScript函數添加到數據庫(){  var inputCountry = document.getElementById("country-field").value;  var inputCity = document.getElementById("city-field").value;  var inputAddress = document.getElementById("address-field").value;  var inputNameofPlace = document.getElementById("placename-field").value;  // Add a new document in collection "cities"   db.collection("UsersShare").doc().set({    name:inputNameofPlace,    city:inputCity,    country:inputCountry,    address:inputAddress,  })  .then(function() {    console.log("Document successfully written!");    document.getElementById("reply").style.display="inline";    })  .catch(function(error) {    console.error("Error writing document: ", error);  });}
查看完整描述

2 回答

?
子衿沉夜

TA貢獻1828條經驗 獲得超3個贊

if(inputCountry.trim() && inputCity.trim() && inputAddress.trim() && 

   inputNameOfPlace.trim()){

  // Add a new document in collection 

  // your code

} else {

 // fields are empty --- error message

}


查看完整回答
反對 回復 2024-01-22
?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

您可以事先檢查字段是否為空,如果不是,則對數據庫進行操作,否則將錯誤記錄到控制臺,就像您的 catch 函數一樣:


if(inputCountry && inputCity && inputAddress && inputNameOfPlace){

    db.coll.....

} else {

    console.error('Field(s) are empty');

}


查看完整回答
反對 回復 2024-01-22
  • 2 回答
  • 0 關注
  • 185 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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