async-await - Angular 9 async-await Uncaught (in promise) 错误
问题描述
//to check the uploaded pdf file total pages > 5
static validatePdfFile(event: any) {
const file = event.target.files[0];
file.arrayBuffer().then(async buff => {
let fileUint8Array = new Uint8Array(buff);
const pdfDoc = await PDFDocument.load(fileUint8Array, {updateMetadata: false});
const totalPages = pdfDoc.getPageCount();
if(totalPages > 5) {
throw new Error('File must be less than 5 pages.');
}
});
}
使用异步等待,得到以下异常:
Uncaught (in promise): Error: File must be less than 5 pages.. Error: File must be less than 5 pages. at Function.
(http://localhost:4200/main.ca25b55f0dfc50ab2.....
从异步抛出新错误的正确方法是什么?
解决方案
:)
validatePdfFile(event: any) {
const file = event.target.files[0];
this.PdfPagesCount(file).subscribe(
(pageCount: number) => {
if (pageCount > 5) {
throw new Error('File must be less than 5 pages.');
} else {
//...
}
},
(error: any) => {
event.target.value = '';
throw error;
}
);
}
countPdfPages(file: File) {
return from(file.arrayBuffer().then(
async buff => {
const fileUint8Array = new Uint8Array(buff);
const pdfDoc = await PDFDocument.load(fileUint8Array, {updateMetadata: false, ignoreEncryption: true});
return pdfDoc.getPageCount();
}
));
}
推荐阅读
- javascript - 刷新页面时从本地存储设置 CSS 样式
- nopcommerce-4.0 - 如何在我自己的支付插件 NopCommerce 上覆盖 EditPopup 方法?
- laravel - 覆盖时的 Voyager 导航栏填充问题
- python - matplotlib 的 Funcanimation 使多个颜色条
- javascript - php搜索表单过滤不显示所有类别
- android - Android ViewGroup 到 ViewHolder
- json - 如何在 Rest Assured 中使用其 id 删除特定参数?
- node.js - 在连接失败时调用 ssh2 结束事件而没有错误事件
- javascript - 从 GeoFire 对象数组中删除重复项
- python - 在 Django 模板中将数字四舍五入到最接近的 1000