【问题标题】:Debug Unhandled Promise Rejections调试未处理的 Promise 拒绝
【发布时间】:2018-03-19 16:17:56
【问题描述】:

我已写入以下数据库查询以取回具有特定偏移量的所有帖子:

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
}

但是,我收到以下Unhandled Promise Rejection

(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.

我的问题是我没有在控制台中获得有关错误的任何进一步信息。

来自您网站的任何建议:

  1. 如何正确调试这些类型的拒绝?
  2. 上面的代码有什么问题?

提前感谢您的回复!

更新

我将函数更改为以下内容:

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

有什么建议我在这里做错了吗?

【问题讨论】:

    标签: javascript node.js promise async-await knex.js


    【解决方案1】:

    记录未处理的拒绝的一种便捷方法是添加如下所示的侦听器(通常在应用的入口点,即 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
    });
    

    【讨论】:

    • 我讨厌节点>:-((((
    • 如果您使用大型应用程序,这真的很有帮助。
    【解决方案2】:

    posts 的定义位置不正确。在try/catch 块之外定义它们或从try 块返回结果:

    async function getPaginationPosts(start, size) {
        try {
            return await knex("posts").select().where({
                deleted: false,
            }).orderBy("createdAt").limit(size).offset(start)
        } catch (e) {
            console.log(e.message)
            console.log(e.stack)
            return null
        }
    }
    

    或者:

    async function getPaginationPosts(start, size) {
        let posts
        try {
            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
    }
    

    【讨论】:

    • 谢谢您或您的回复 alexmac。我调整了我的功能。但是,我仍然收到 PromiseRejection。任何建议我在这里做错了什么以及如何正确调试这些类型的拒绝?提前谢谢!
    • 看起来第二个错误与getPaginationPosts函数无关。你还和getPaginationPosts一起调用了哪些其他函数?此外,请确保标记为async 的所有函数代码都在try/catch 块中。检查每个异步函数。
    猜你喜欢
    • 2017-11-20
    • 1970-01-01
    • 2016-11-10
    • 2019-04-15
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多