我在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
}
料青山看我应如是
TA贡献1772条经验 获得超8个赞
您可以事先检查字段是否为空,如果不是,则对数据库进行操作,否则将错误记录到控制台,就像您的 catch 函数一样:
if(inputCountry && inputCity && inputAddress && inputNameOfPlace){
db.coll.....
} else {
console.error('Field(s) are empty');
}
- 2 回答
- 0 关注
- 97 浏览
添加回答
举报
0/150
提交
取消