javascript - 在强大的 form.parse 中, throw 关键字无法正常工作
问题描述
使用 throw 关键字,每次我点击路径时它都不起作用,总是给我指向单词 throw 关键字的错误,如果我删除 throw 它的工作检查代码以供参考,我在 server.js 中有我的自定义错误处理程序,它与其他导出工作正常
exports.createMainCategory = asyncHandler(async (req, res) => {
let form = new formidable.IncomingForm();
form.keepExtensions = true;
form.parse(req, (err, fields, file) => {
if (err) {
res.status(400);
throw new Error("problem with image");
}
const { maincategory } = fields;
if (!maincategory) {
//NOT WORKING WITH THIS
res.status(400);
throw new Error("include all files");
// ONLY WORKIN WITH BELOW CODE
// return res.status(400).json({
// error: "Please include all fields",
// });
}
let mainCat = new MainCat(fields);
//handle file here
if (file.photo) {
if (file.photo.size > 3000000) {
res.status(400);
throw new Error("File size too big!");
}
mainCat.photo.data = fs.readFileSync(file.photo.path);
mainCat.photo.contentType = file.photo.type;
}
//save to the DB
mainCat.save((err, mcategory) => {
if (err) {
res.status(400).json({
error: "Saving category in DB failed",
});
}
res.json(mcategory);
});
});
});
解决方案
推荐阅读
- linux - 如何在 Docker 容器中运行 Nginx 并将其设置为每 6 小时重新加载一次配置?
- python-3.x - 有没有办法让python中的输入通道等于过滤器的维度?
- tensorflow - 将冻结模型“.pb”文件转换为“.tflite”文件所需的参数 input_arrays 和 output_arrays 是什么?
- python - 处理 Excel 格式的数字
- html - 如何仅使用 HTML 和 CSS 使元素的背景切换复选框?
- javascript - 运行应用程序时控制台中没有任何内容,但带有 mongodb 的终端显示连接?
- web-scraping - VBScript 将每一行字符串与每一行文本文件进行比较并进行替换
- java - 将客户端应用程序连接到我用 java 制作的服务器
- node.js - 无法使用 ffmpeg 将视频与 express 合并:无法使用“in”运算符在未定义中搜索“可读”
- python - 如何将此基于列表的代码转换为基于numpy数组的代码?