Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug Unhandled Promise Rejections

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?

like image 479
Carol.Kar Avatar asked Jan 30 '26 03:01

Carol.Kar


2 Answers

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
});
like image 185
coockoo Avatar answered Jan 31 '26 17:01

coockoo


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
}
like image 43
alexmac Avatar answered Jan 31 '26 19:01

alexmac



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!