我已写入以下数据库查询以获取具有特定偏移量的所有帖子:
async function getPaginationPosts(start, size) {
try {
const posts = await knex("posts").select().where({
deleted: false,
}).orderBy("createdAt").limit(size).offset(start)
} catch (e) {
console.log(e.message)
console.log(e.stack)
}
return posts
}
但是,我收到以下未处理的 promise 拒绝
(node:1824) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: posts is n
ot defined
(node:1824) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejection
s that are not handled will terminate the Node.js process with a non-zero exit code.
我的问题是我没有在控制台中获得有关错误的任何进一步信息。
来自您网站的任何建议:
- 如何正确调试这些类型的拒绝?
- 上面的代码有什么问题?
提前感谢您的回复!
更新
我将函数更改为以下内容:
async function getPaginationPosts(size, offset) {
try {
return await knex("posts").select().where({
deleted: false,
}).orderBy("createdAt").limit(size).offset(offset)
} catch (e) {
console.log(e.message)
console.log(e.stack)
return null
}
}
现在我遇到以下异常:
(node:9096) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: start is n
ot defined
我在函数中没有使用变量start
。
有什么建议我在这里做错了什么吗?
最佳答案
记录未处理拒绝的一种便捷方法 - 是添加如下所示的监听器(通常在应用程序的入口点,即 main.js)
process.on("unhandledRejection", (error) => {
console.error(error); // This prints error with stack included (as for normal errors)
throw error; // Following best practices re-throw error and let the process exit with error code
});
关于javascript - 调试未处理的 Promise 拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46629778/