Unhandled Promise rejection typescript and Node.js

Asked

Viewed 2,287 times

-1

Good night to you all! I am developing an app with React, Node and typescript and was programming a route with post method for inserting data using sqlite database and testing in India but when running the file it returns me an error of Unhandled Promise rejection and I do not know how to treat this event.

this and my route:

routes.post('/points', async(request, response)=>{

    const {
        name,
        email,
        whatsapp,
        latitude,
        longitude,
        city,
        uf,
        items
    }=request.body;


   await knex('points').insert({
        image: 'image_fake',
        name,
        email,
        whatsapp,
        latitude,
        longitude,
        city,
        uf,

    })
    return response.json({sucess: true});

});

in India I make a request of the post type and the json body as follows

{
    "name": "Mercado Livre",
    "email": "[email protected]",
    "whatsapp": "3198774664",
    "latitude": "-45.4892729",
    "longitude": "-35.4324233",
    "city": "BH",
    "uf": "MG",
    "items": [
        1,
        2,
        6
    ]
}

the expected was for me to have an "sucess: true;" response according to my Return Response.json({sucess: true});

but he returns me the following:

(Ode:13488) Unhandledpromiserejectionwarning: Error: Insert into points (city, email, image, latitude, longitude, name, uf, whatsapp) values ('BH', '[email protected]', 'image_fake', '-45.4892729', '-35.4324233', 'Mercado Livre', 'MG', '3198774664') - SQLITE_ERROR: table points has no column named Whatsapp (Ode:13488) Unhandledpromiserejectionwarning: Unhandled Promise rejection. This error either originated by Throwing Inside of an async Function without a catch block, or by rejecting a Promise which was not handled with . (catch). To terminate the Node process on unhandled Promise rejection, use the CLI flag --unhandled-rejections=strict (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2) (Ode:13488) [DEP0018] Deprecationwarning: Unhandled Promise rejections are deprecated. In the Future, Promise rejections that are not handled will terminate the Node.js process with a non-zero Exit code.

could someone help me?

1 answer

0

Express does not have a method for capturing bounces of promises natively. Therefore, how are you using async/await, should always ensure that no rejected promise "get out" from inside the router, since there is no other mechanism to treat it.

You can use blocks try/catch:

routes.post('/points', async (request, response) => {
  try {
    const {
      name,
      email,
      // ...
    } = request.body;

    await knex('points').insert({
      name,
      email,
      // ...
    });

    return response.json({ sucess: true });
  } catch (error) {
    response.json({
      error: true,
      message: error.message
    }); 
  }
});

In your specific case, the error is in trying to insert a disgusting record in points. According to the error message, there is no column whatsapp on the table:

SQLITE_ERROR: table points has no column named whatsapp

There are other ways to solve this problem, such as using some higher order function (Higher order Function), which will make a more generic error handling, or chain the catch directly in the method that returns to Promise.

To learn more about the warning unhandledPromiseRejection, read this answer.


I also put a semicolon at the end of a missing line. It may seem "freshness", but this avoids some bizarre situations that can occur if you don’t use them (see more about this here).

  • Try catch helped a lot to identify the error and with that I could notice that I had created the name of the wrong Whatsapp variable in the table and with that gave error in insertion.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.