我有一个提交功能
const onSubmit = async (creditCardInput) => {
const { navigation } = this.props;
this.setState({ submitted: true });
let creditCardToken;
try {
creditCardToken = await getCreditCardToken(creditCardInput);
if (creditCardToken.error) {
this.setState({ submitted: false});
return;
}
} catch (e) {
this.setState({ submitted: false });
return;
}
try {
const obj = await subscribeUser(creditCardToken);
console.log('returned obj', obj);
try {
const docRef = await this.db.collection("users").doc(this.user.email).set({
id: this.user.uid,
subscriptionId: obj.customerId,
active: true
});
console.log("Document written with ID: ", docRef);
this.navigateToScreen('HomeScreen');
} catch (e) {
console.log("Error adding document: ", e);
}
} catch (error) {
console.log('catch error', error);
this.setState({ submitted: false, error: SERVER_ERROR, message: error });
}
};
这是可疑的-当这。db。集合调用失败实现捕获意味着它将注销“添加文档时出错:”,请参阅下面的代码段
try {
const docRef = await this.db.collection("users").doc(this.user.email).set({
id: this.user.uid,
subscriptionId: obj.customerId,
active: true
});
console.log("Document written with ID: ", docRef);
this.navigateToScreen('HomeScreen');
} catch (e) {
console.log("Error adding document: ", e);
}
但是,当我以不同的方式实现subscribe user函数时(见下文),我不再使用try-catch,而是使用。然后在wait上,内部函数最终失败,执行外部catch语句,这意味着它将从控制台注销。日志('catch error',error);当它应该注销控制台时。日志(“添加文档时出错:”,错误);为什么呢?它们不应该以相同的方式工作吗?这意味着上面和下面的代码片段应该以相同的方式工作,请参见下面的代码
await subscribeUser(creditCardToken).then((obj)=> {
this.db.collection("users").doc(this.user.email).set({
id: this.user.uid,
subscriptionId: obj.customerId,
active: true
}).then((docRef) => {
console.log("Document written with ID: ", docRef);
this.navigateToScreen('HomeScreen');
}).catch((errors) => {
console.log("Error adding document: ", errors);
});
}).catch((error) => {
console.log('catch error', error);
this.setState({ submitted: false message: error });
});
当this.db.collections成功解析.然后执行它应该意味着它用console.log(用ID写的文档:,docRef)注销的方式时,只是一个附加的注意事项,但是就像我之前说过的,如果它被拒绝,则执行外部捕获而不是内心的渴望
此外,向this.db.collection函数添加返回和删除等待对结果没有影响
有两种方法可以处理此问题-将set
函数转换为使用async/await语法,或将set函数返回到subscribeeuser
函数的next-then语句。我将两者都展示,但我更喜欢第二个,因为我不喜欢将async/await与then/catch混合使用。
const tempThis = this
subscribeUser(creditCardToken).then((obj)=> {
// RETURN this promise and you'll get it in the next then statement
return tempThis.db.collection("users").doc(tempThis.user.email).set({
id: tempThis.user.uid,
subscriptionId: obj.customerId,
active: true
})
}).then((docRef) => {
console.log("Document written with ID: ", docRef);
this.navigateToScreen('HomeScreen');
}).catch((errors) => {
// This will catch the errors from subscribeUser AND from the set function
console.log("Error adding document: ", errors);
});
或者只使用异步/等待
try {
const obj = await subscribeUser(creditCardToken)
const docRef = await this.db.collection("users").doc(this.user.email).set({
id: this.user.uid,
subscriptionId: obj.customerId,
active: true
})
console.log("Document written with ID: ", docRef);
this.navigateToScreen('HomeScreen');
} catch (error) {
// Handle your errors
}
问题内容: 我经常遇到如下情况: 仍然需要尝试-最终在内部捕获块。 克服此问题的最佳实践是什么? 问题答案: 写一个类,其中包含捕获和记录此类异常的方法,然后根据需要使用。 您最终会看到如下内容: 您的客户端代码将类似于: 更新: 自Java 7开始,各种JDBC接口都得到了扩展,而以上代码回答了原始问题,如果您直接针对JDBC API编写代码,则现在可以对其进行结构化:
问题内容: 我正在研究节点7异步/等待功能,并不断跨这样的代码绊脚 这似乎是使用异步/等待解决/拒绝或返回/抛出的唯一可能性,但是,v8不会在try / catch块中优化代码吗? 有其他选择吗? 问题答案: 备择方案 替代方法: 显式地使用诺言将是这样的: 或类似的东西,使用延续传递样式: 原始例子 您的原始代码所做的是暂停执行并等待由其返回的诺言解决。然后,它继续执行,并将返回的值写入,如果承
我使用async、await编写了一个Javascript代码,并在每个异步函数中使用try、catch。 假设如果我写了10个异步函数,那么我需要在所有这10个函数中写try,catch。现在我脑子里有一个问题,那就是它不会造成性能开销吗?
我正在与async Wait try catch块斗争几天。 这个异步函数中的try-catch是否正确? 这就是我创建自定义错误类并全局导出的方式。 要求: 故意换了工作。我想找份工作。国际直拨电话 这样我就能抓住错误。如果有错误,则抛出新创建的自定义错误类。但抛出队列错误将导致日志记录 同样,即使不需要捕捉那个里的错误,因为try块在工作,若我抛出QueueError,我只想捕捉最后一个cat
我有一个存储过程似乎没有正确记录错误。 代码有错误,但 catch 块似乎未生效。 try块相当长,但错误部分很简单,并且在最后出现,所以我已经对此进行了预测。 proc失败的错误是我们的老朋友“列名或提供的值的数量与表定义不匹配”。我已经修复了这个错误 - 这是一个愚蠢的懒惰错误 - 但我感到困惑为什么我的错误日志记录过程似乎没有工作 - 没有行入到我的 ExtractsErrorLog 表中。
问题内容: 我正在尝试构建一个发出ajax请求的Google Chrome扩展程序。与GMail Checker扩展程序类似。问题是,当我使用jquery进行请求时,并输入了错误的用户名/密码时,它会静默失败,并忽略错误回调函数。 如果我将ajax调用从background.html脚本(在开发人员窗口中看不到请求)中移出到options.html脚本中,则会得到一个对话框来重新进行身份验证。如果
我刚开始玩硒,我正在寻找一些帮助。在我试图测试的网页上,我有一个搜索按钮,当页面加载时,我有一个html表,显示在它下面的结果。 搜索结果表的html如下所示... 使用selenium IDE,我能够创建一个junit测试来执行屏幕快照中的搜索,但我很难找到如何修改单元测试,以便正确地等待搜索完成,然后在页面上返回结果后,单击搜索结果表第一行中的第一项。
问题内容: 我喜欢Typescript等中提供的新功能的平坦性。但是,我不确定我是否必须在块的外部声明要输入的变量以便以后使用,这一点我不确定。像这样: 如果我错了,请纠正我,但是似乎最好的做法是不要在主体中放置多行业务逻辑,因此,我只剩下在块外声明,在块中分配,以及然后使用它。 在这种情况下,最佳做法是什么? 问题答案: 似乎最好的做法是不要在try主体中放置多行业务逻辑 其实我会说是的。通常,