我在cloud firestor中創(chuàng)建了一個(gè)數(shù)據(jù)庫,所以現(xiàn)在我想通過輸入字段向其中添加一些信息,當(dāng)輸入字段為空信息時(shí)存儲(chǔ)問題并且catch函數(shù)不起作用,我該如何修復(fù)它,并進(jìn)行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函數(shù)添加到數(shù)據(jù)庫(){ 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貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊
if(inputCountry.trim() && inputCity.trim() && inputAddress.trim() &&
inputNameOfPlace.trim()){
// Add a new document in collection
// your code
} else {
// fields are empty --- error message
}

料青山看我應(yīng)如是
TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以事先檢查字段是否為空,如果不是,則對數(shù)據(jù)庫進(jìn)行操作,否則將錯(cuò)誤記錄到控制臺(tái),就像您的 catch 函數(shù)一樣:
if(inputCountry && inputCity && inputAddress && inputNameOfPlace){
db.coll.....
} else {
console.error('Field(s) are empty');
}
- 2 回答
- 0 關(guān)注
- 183 瀏覽
添加回答
舉報(bào)
0/150
提交
取消