最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Debug Unhandled Promise Rejections - Stack Overflow

programmeradmin1浏览0评论

I have written to following db query to get back all posts with a certain offset:

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
}

However, I am getting the following 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.

My problem is that I do not get any further information about the error in the console.

Any suggestions from your site:

  1. How to debug these types of rejections properly?
  2. What is wrong with the above code?

Thank you in advance for your replies!

Update

I changed my function to the following:

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
    }
}

Now I am getting the following exception:

(node:9096) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: start is n
ot defined

I do not use a variable start in my function.

Any suggestions what I am doing wrong here?

I have written to following db query to get back all posts with a certain offset:

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
}

However, I am getting the following 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.

My problem is that I do not get any further information about the error in the console.

Any suggestions from your site:

  1. How to debug these types of rejections properly?
  2. What is wrong with the above code?

Thank you in advance for your replies!

Update

I changed my function to the following:

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
    }
}

Now I am getting the following exception:

(node:9096) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): ReferenceError: start is n
ot defined

I do not use a variable start in my function.

Any suggestions what I am doing wrong here?

Share Improve this question edited Oct 8, 2017 at 10:35 Carol.Kar asked Oct 8, 2017 at 9:51 Carol.KarCarol.Kar 5,35537 gold badges148 silver badges294 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

A convenient way to log unhandled rejections - is to add listener (usually at entry point of your app, i.e. main.js) that looks like this

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
});

posts are defined not in the correct place. Define them outside of try/catch block or return result from try block:

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
    }
}

Or:

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
}
发布评论

评论列表(0)

  1. 暂无评论