Skip to content
This repository was archived by the owner on Mar 11, 2022. It is now read-only.

Commit 7aabe30

Browse files
committed
Add callback signature notes to README.md
1 parent c785ad6 commit 7aabe30

File tree

1 file changed

+81
-5
lines changed

1 file changed

+81
-5
lines changed

README.md

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ This is the official Cloudant library for Node.js.
88
* [Getting Started](#getting-started)
99
* [Initialization](#initialization)
1010
* [Initialization Callback](#initialization-callback)
11+
* [Callback Signature](#callback-signature)
1112
* [Request Plugins](#request-plugins)
1213
* [API Reference](#api-reference)
1314
* [Authorization and API Keys](#authorization-and-api-keys)
@@ -220,13 +221,88 @@ Cloudant({account:me, password:password}, function(err, cloudant) {
220221
});
221222
~~~
222223

223-
After initialization, in general, callback functions receive three arguments:
224+
### Callback Signature
224225

225-
* `err` - the error, if any
226-
* `body` - the http _response body_ from Cloudant, if no error.
227-
* `header` - the http _response header_ from Cloudant, if no error
226+
Callback functions receive three arguments:
228227

229-
The `ping()` function is the only exception to this rule. It does not return headers since a "ping" is made from multiple requests to gather various bits of information.
228+
```js
229+
function(err, body, header) {}
230+
```
231+
232+
* `err` - The _error_ (if any). For example, fetching a document that doesn't exist:
233+
234+
```js
235+
var mydb = cloudant.db.use('mydb');
236+
mydb.get('non-existent-doc', function(err, data) {
237+
console.log(err);
238+
});
239+
```
240+
241+
```
242+
{ Error: deleted
243+
at Object.clientCallback (/usr/src/app/node_modules/nano/lib/nano.js:248:15)
244+
at Request._callback (/usr/src/app/node_modules/@cloudant/cloudant/lib/clientutils.js:154:11)
245+
...
246+
name: 'Error',
247+
error: 'not_found',
248+
reason: 'deleted',
249+
scope: 'couch',
250+
statusCode: 404,
251+
request:
252+
{ method: 'GET',
253+
headers:
254+
{ 'content-type': 'application/json',
255+
accept: 'application/json' },
256+
uri: 'http://localhost:5984/_users/895c3440-42e7-11e8-b9b2-358fa5dee4a0' },
257+
headers:
258+
{ 'x-couchdb-body-time': '0',
259+
'x-couch-request-id': '1c16b2b81f',
260+
'transfer-encoding': 'chunked',
261+
etag: '"7Q4MT2X8W1RO3JQOLSA4KGMV7"',
262+
date: 'Fri, 27 Apr 2018 08:49:26 GMT',
263+
'content-type': 'application/json',
264+
'cache-control': 'must-revalidate',
265+
statusCode: 404,
266+
uri: 'http://localhost:5984/_users/895c3440-42e7-11e8-b9b2-358fa5dee4a0' },
267+
errid: 'non_200',
268+
description: 'couch returned 404' }
269+
```
270+
271+
As shown above, the corresponding database `request`, `headers` and `statusCode` are also returned in the error.
272+
273+
* `body` - The HTTP _response body_ (if no error). For example:
274+
275+
```js
276+
cloudant.db.list(function(err, body, header) {
277+
console.log(body);
278+
});
279+
```
280+
281+
```
282+
[ '_replicator', '_users' ]
283+
```
284+
285+
* `header` - The HTTP _response header_ (if no error). For example:
286+
287+
```js
288+
cloudant.db.list(function(err, body, header) {
289+
console.log(header);
290+
});
291+
```
292+
293+
```
294+
{ 'x-couchdb-body-time': '0',
295+
'x-couch-request-id': '591be401f1',
296+
'transfer-encoding': 'chunked',
297+
etag: '"7Q4MT2X8W1RO3JQOLSA4KGMV7"',
298+
date: 'Fri, 27 Apr 2018 08:49:49 GMT',
299+
'content-type': 'application/json',
300+
'cache-control': 'must-revalidate',
301+
statusCode: 200,
302+
uri: 'http://localhost:5984/_all_dbs' }
303+
```
304+
305+
Note that the `statusCode` and `uri` and also included amongst the response headers.
230306

231307
### Request Plugins
232308

0 commit comments

Comments
 (0)