Skip to content

Commit 79b816b

Browse files
committed
docs: add throw error in all query callbacks
closes #1585
1 parent d16b003 commit 79b816b

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

Readme.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ var mysql = require('mysql');
166166
var connection = mysql.createConnection(...);
167167

168168
connection.query('SELECT 1', function (error, results, fields) {
169-
// connected! (unless `error` is set)
169+
if (error) throw error;
170+
// connected!
170171
});
171172
```
172173

@@ -352,6 +353,9 @@ pool.getConnection(function(err, connection) {
352353
// And done with the connection.
353354
connection.release();
354355

356+
// Handle error after the release.
357+
if (error) throw error;
358+
355359
// Don't use the connection here, it has been returned to the pool.
356360
});
357361
});
@@ -612,6 +616,7 @@ provided data before using it inside a SQL query. You can do so using the
612616
var userId = 'some user provided value';
613617
var sql = 'SELECT * FROM users WHERE id = ' + connection.escape(userId);
614618
connection.query(sql, function (error, results, fields) {
619+
if (error) throw error;
615620
// ...
616621
});
617622
```
@@ -621,6 +626,7 @@ like to have escaped like this:
621626

622627
```js
623628
connection.query('SELECT * FROM users WHERE id = ?', [userId], function (error, results, fields) {
629+
if (error) throw error;
624630
// ...
625631
});
626632
```
@@ -631,6 +637,7 @@ in the following query `foo` equals `a`, `bar` equals `b`, `baz` equals `c`, and
631637

632638
```js
633639
connection.query('UPDATE users SET foo = ?, bar = ?, baz = ? WHERE id = ?', ['a', 'b', 'c', userId], function (error, results, fields) {
640+
if (error) throw error;
634641
// ...
635642
});
636643
```
@@ -692,6 +699,7 @@ provided by a user, you should escape it with `mysql.escapeId(identifier)`,
692699
var sorter = 'date';
693700
var sql = 'SELECT * FROM posts ORDER BY ' + connection.escapeId(sorter);
694701
connection.query(sql, function (error, results, fields) {
702+
if (error) throw error;
695703
// ...
696704
});
697705
```
@@ -720,6 +728,7 @@ like to have escaped like this:
720728
var userId = 1;
721729
var columns = ['username', 'email'];
722730
var query = connection.query('SELECT ?? FROM ?? WHERE id = ?', [columns, 'users', userId], function (error, results, fields) {
731+
if (error) throw error;
723732
// ...
724733
});
725734

@@ -955,6 +964,7 @@ the table name like this:
955964
```js
956965
var options = {sql: '...', nestTables: true};
957966
connection.query(options, function (error, results, fields) {
967+
if (error) throw error;
958968
/* results will be an array like this now:
959969
[{
960970
table1: {
@@ -975,6 +985,7 @@ Or use a string separator to have your results merged.
975985
```js
976986
var options = {sql: '...', nestTables: '_'};
977987
connection.query(options, function (error, results, fields) {
988+
if (error) throw error;
978989
/* results will be an array like this now:
979990
[{
980991
table1_fieldA: '...',
@@ -1206,6 +1217,7 @@ Or on the query level:
12061217
```js
12071218
var options = {sql: '...', typeCast: false};
12081219
var query = connection.query(options, function (error, results, fields) {
1220+
if (error) throw error;
12091221
// ...
12101222
});
12111223
```

0 commit comments

Comments
 (0)