Javascript 如果.then失败,如何执行语句

Javascript 如果.then失败,如何执行语句,javascript,database,firebase,google-cloud-firestore,Javascript,Database,Firebase,Google Cloud Firestore,我需要它像if-else语句一样执行。考虑。然后是如果,我能做些什么来获得其他 上面的代码显示了一个错误 “无法读取未定义的属性“catch” 更新:我将.catch放在then函数中。它应该在后面直接发送,抱歉输入错误 使用。接住。然后。catch方法为我们提供了一个错误对象 firestore.collection("Something").where("User2", "==", "") .get() .then(function(querySnapshot) {

我需要它像if-else语句一样执行。考虑。然后是如果,我能做些什么来获得其他

上面的代码显示了一个错误

“无法读取未定义的属性“catch”


更新:我将.catch放在then函数中。它应该在后面直接发送,抱歉输入错误

使用。接住。然后。catch方法为我们提供了一个错误对象

   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,

          })
    });
    })

您还可以在try-catch块中为此使用异步和等待语法

异步函数getRecords(){ 试一试{ const records=await firestore.collection(“某物”)。其中(“User2”,“==”,“”) .get(); //将记录作为数组进行处理 记录。forEach(功能(文档){ const docRef=firestore.collection(“某物”).doc(doc.id); docRef.update({ //一组语句,如果.then为true }) } } 捕获(e){ console.log(“错误”,例如消息); //在这里处理您的异常 }
}
.catch
如果我知道我的承诺,你确定你的函数会返回一个承诺吗?函数在。然后运行吗?如果函数不运行,我想。catch开始工作。可能吗?可能只是我的一个输入错误,我会更新答案什么时候会发生。哪里不是真的,will。catch work?我想你必须检查你的内部。然后,如果您有一个文档。例如,(!doc){//如果找不到文档,则执行操作。}只有在对数据库的查询失败时,.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
 });