mysql - npm mysql 库中抛出的错误导致崩溃 - throw err; // 重新抛出非 MySQL 错误
问题描述
我现在一直在将该mysql
库用于一个项目。我注意到了一些东西。
try {
let number = req.params.number;
number = parseInt(number, 10);
if (number && number % 10 == 0 && number > 0) {
pool.query(
`select * from posts limit ${number - 10}, ${10}`,
function (err, result) {
if (err) {
throw "Url incorrect, ensure that the params % 10 == 0";
}
// if (!result.length) {
// throw "No more posts available";
// }
const response = result.map(function (item) {
return {
...item,
files: item.files
? item.files.split(",").map(function (item) {
return (
"http://localhost:4000/files/" +
item
);
})
: [],
};
});
res.json({
valid: true,
data: response,
nextPage: number + 10,
});
}
);
} else {
throw "Something went wrong";
}
} catch (err) {
console.log(err);
res.json({ valid: false, msg: err });
}
这是我的代码的一部分。在这里,当结果的长度为 0 时,我会抛出错误并将其 console.log 到 catch 块中。但我注意到它使程序崩溃并throw err; // Rethrow non-MySQL errors
^
No more posts available
(Use `node --trace-uncaught ...` to show where the exception was thrown)
npm ERR! code ELIFECYCLE.......
在终端中打印。我不明白我犯了什么错误?
解决方案
推荐阅读
- sql - 查询在 From 子句错误中给了我一个语法错误
- php - 以编程方式将干预图像包含到 Wordpress 主题中(不使用插件)
- xcode - 有没有办法在 Fastlane 快速文件中设置 -UseModernBuildSystem=NO?
- c# - 将 Force.com 用于任意 REST API 方法
- ios - 如何在 NativeScript 中以编程方式创建 UIImageView?
- yii2 - yii2 检索 AccessTokenByUser 的密码哈希
- ios - ios - 使用 Firebase 分页时重复键
- python - 有什么方法可以防止修改 ndarray 子类的内容?
- python - 子图过于紧凑
- c++ - 为函数分配空间然后从函数内删除空间是不好的做法吗