Node js - Promise Rejection Warning when process a

2019-01-29 09:42发布

问题:

I'm creating a feature that when you pass a bar code, the product of bar code is added to a table.

After that we have a button to save the data in table. For each row is necessary to generate a stamp and an insert into a table named bo, bo2 and bo3.

With the stamp of bo we will do an insert into a table named bi, bi2 and sl.

So, I created a structure to do this with promises but I have the error but it just happens when I have for example 20 rows:

<node:6796> UnhandledPromiseRejectionWarning: Unhandled promise rejection <rejection id: 13>: RequestError: Transaction <Process ID 61> was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.

The connection to database is done with mssql library.

Like my function is bigger and it is necessary to show all code to you understand better the problem I put the link below of my function. http://pastebin.com/MnM6AE6a

I already changed the structure of my function but the problem continue.

回答1:

Your code example is 150 lines of code so I won't debug all of that (you should always provide a minimal code example needed to reproduce your problem and include in the question if you want people to help you spot and fix the actual errors) but the UnhandledPromiseRejectionWarning means that somewhere you didn't include a catch handler for a promise.

For example, if anywhere you have:

promise.then(func);

instead of:

promise.then(func1, func2);

or:

promise.then(func1).catch(func2);

Then your code is not halndling the promise rejections properly and as such is broken. In the future this program may not even start in next versions of Node - I explained it in more details in this answer:

  • Should I refrain from handling Promise rejection asynchronously?

Also, having looked at your code I strongly suggest reading about SQL injection attacks. I mean it. Google the subject and fix your code even before you fix the problem with unhandled promise rejections. Seriously. I'm not referring to this comic strip any more these years but every developer should know it and understand:

Please read:

  • https://en.wikipedia.org/wiki/SQL_injection
  • http://www.beyondsecurity.com/about-sql-injection.html
  • http://projects.webappsec.org/w/page/13246963/SQL%20Injection
  • http://bobby-tables.com/