@@ -166,7 +166,8 @@ var mysql = require('mysql');
166
166
var connection = mysql .createConnection (... );
167
167
168
168
connection .query (' SELECT 1' , function (error , results , fields ) {
169
- // connected! (unless `error` is set)
169
+ if (error) throw error;
170
+ // connected!
170
171
});
171
172
```
172
173
@@ -352,6 +353,9 @@ pool.getConnection(function(err, connection) {
352
353
// And done with the connection.
353
354
connection .release ();
354
355
356
+ // Handle error after the release.
357
+ if (error) throw error;
358
+
355
359
// Don't use the connection here, it has been returned to the pool.
356
360
});
357
361
});
@@ -612,6 +616,7 @@ provided data before using it inside a SQL query. You can do so using the
612
616
var userId = ' some user provided value' ;
613
617
var sql = ' SELECT * FROM users WHERE id = ' + connection .escape (userId);
614
618
connection .query (sql, function (error , results , fields ) {
619
+ if (error) throw error;
615
620
// ...
616
621
});
617
622
```
@@ -621,6 +626,7 @@ like to have escaped like this:
621
626
622
627
``` js
623
628
connection .query (' SELECT * FROM users WHERE id = ?' , [userId], function (error , results , fields ) {
629
+ if (error) throw error;
624
630
// ...
625
631
});
626
632
```
@@ -631,6 +637,7 @@ in the following query `foo` equals `a`, `bar` equals `b`, `baz` equals `c`, and
631
637
632
638
``` js
633
639
connection .query (' UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?' , [' a' , ' b' , ' c' , userId], function (error , results , fields ) {
640
+ if (error) throw error;
634
641
// ...
635
642
});
636
643
```
@@ -692,6 +699,7 @@ provided by a user, you should escape it with `mysql.escapeId(identifier)`,
692
699
var sorter = ' date' ;
693
700
var sql = ' SELECT * FROM posts ORDER BY ' + connection .escapeId (sorter);
694
701
connection .query (sql, function (error , results , fields ) {
702
+ if (error) throw error;
695
703
// ...
696
704
});
697
705
```
@@ -720,6 +728,7 @@ like to have escaped like this:
720
728
var userId = 1 ;
721
729
var columns = [' username' , ' email' ];
722
730
var query = connection .query (' SELECT ?? FROM ?? WHERE id = ?' , [columns, ' users' , userId], function (error , results , fields ) {
731
+ if (error) throw error;
723
732
// ...
724
733
});
725
734
@@ -955,6 +964,7 @@ the table name like this:
955
964
``` js
956
965
var options = {sql: ' ...' , nestTables: true };
957
966
connection .query (options, function (error , results , fields ) {
967
+ if (error) throw error;
958
968
/* results will be an array like this now:
959
969
[{
960
970
table1: {
@@ -975,6 +985,7 @@ Or use a string separator to have your results merged.
975
985
``` js
976
986
var options = {sql: ' ...' , nestTables: ' _' };
977
987
connection .query (options, function (error , results , fields ) {
988
+ if (error) throw error;
978
989
/* results will be an array like this now:
979
990
[{
980
991
table1_fieldA: '...',
@@ -1206,6 +1217,7 @@ Or on the query level:
1206
1217
``` js
1207
1218
var options = {sql: ' ...' , typeCast: false };
1208
1219
var query = connection .query (options, function (error , results , fields ) {
1220
+ if (error) throw error;
1209
1221
// ...
1210
1222
});
1211
1223
```
0 commit comments