javascript - 我在 JScript 上遇到未经处理的拒绝错误
问题描述
这是一个去中心化存储网络的 github 脚本,但是每当我上传时,我在 await() 函数中都会收到这个 Unhandled Rejection 错误。
handleSubmit = async (e) => {
e.preventDefault();
if (this.state.file !== "") {
this.setState({ loading: true });
await this.state.files.add(this.state.file,this.state.file.name);
this.setState({ loading: false });
this.getALLHashes();
}
else {
this.setState({ fieldReq: true })
}
}
我是 JavaScript 新手,所以我不知道如何解决这个问题。请帮忙!!
解决方案
这意味着,如果发生错误,您将没有任何机制来处理该拒绝。使用try / catch
. 像这样试试
handleSubmit = async e => {
e.preventDefault();
if (this.state.file !== "") {
try {
this.setState({ loading: true });
await this.state.files.add(this.state.file, this.state.file.name);
this.setState({ loading: false });
this.getALLHashes();
} catch (err) {
console.log(err);
}
} else {
this.setState({ fieldReq: true });
}
};
推荐阅读
- javascript - 使用 debounce 响应搜索
- node.js - 如何修复错误 TS2694:命名空间 '".../node_modules/@angular/core/core"' 在角度更新后没有导出成员 'ɵɵNgModuleDefWithMeta'?
- python - python遍历文件并替换字符串
- javascript - 从 React 中的状态对象数组中添加/删除复选框的元素
- android - 我想过滤 Firebase 数据并仅在 kotlin 的 recyclerview 上显示通过电子邮件过滤的用户
- ios - 多目标项目中的 Xcode 共享框架链接问题
- javascript - transform: scale(x) 是一项昂贵的操作吗?
- sql-server - SQL:递归更新表中的 xml 列
- html - 无序列表是堆叠的,而不是彼此相邻的
- android-recyclerview - 具有多个视图类型的 RecyclerView-selection