firestore.collection("Something").where("User2", "==", "")
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
const docRef = firestore.collection("Something").doc(doc.id);
docRef.update({
User2: messageseqno,
})
})
.catch(error => {
console.log(error);
const docRef = firestore.collection("Something").doc();
var nullvalue = "";
docRef.update({
User1: messageseqno,
User2: nullvalue,
})
});
})
我需要这个像if-else语句一样执行。考虑。然后,如果,我该怎么办才能得到别的。
上面的代码显示了一个错误
“无法读取未定义的属性'catch'
更新:我把.catch放在then-function中。它应该是直接来的,抱歉打字错误。
.then之后使用.catch。 catch方法为我们提供了一个错误对象。
firestore.collection("Something").where("User", "==", input)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
const docRef = firestore.collection("Something").doc(doc.id);
docRef.update({
//set of statements if .then is true
})
})
})
.catch(error => {
console.log(error);
// Do stuff when query fails
});
您还可以在try-catch块中使用async和await语法
async function getRecords() {
try {
const records = await firestore.collection("Something").where("User2", "==", "")
.get();
//do stuff with records as an array
records.forEach(function(doc) {
const docRef = firestore.collection("Something").doc(doc.id);
docRef.update({
//set of statements if .then is true
})
}
}
catch(e) {
console.log("Error",e.message);
//handle your exceptions here
}
}