| |
| {% include anchor.html edit="true" title="Fetch a document" hash="fetch_document"%} |
| |
| {% highlight js %} |
| db.get(docId, [options], [callback]) |
| {% endhighlight %} |
| |
| Retrieves a document, specified by `docId`. |
| |
| ### Options |
| |
| All options default to `false` unless otherwise specified. |
| |
| * `options.rev`: Fetch specific revision of a document. Defaults to winning revision (see [the CouchDB guide](http://guide.couchdb.org/draft/conflicts.html)). |
| * `options.revs`: Include revision history of the document. |
| * `options.revs_info`: Include a list of revisions of the document, and their availability. |
| * `options.open_revs`: Fetch all leaf revisions if `open_revs="all"` or fetch all leaf revisions specified in `open_revs` array. Leaves will be returned in the same order as specified in input array. |
| * `options.conflicts`: If specified, conflicting leaf revisions will be attached in `_conflicts` array. |
| * `options.attachments`: Include attachment data. |
| * `options.binary`: Return attachment data as Blobs/Buffers, instead of as base64-encoded strings. |
| * `options.latest`: Forces retrieving latest “leaf” revision, no matter what rev was requested. Default is `false`. |
| |
| #### Example Usage: |
| |
| {% include code/start.html id="get1" type="callback" %} |
| {% highlight js %} |
| db.get('mydoc', function(err, doc) { |
| if (err) { return console.log(err); } |
| // handle doc |
| }); |
| {% endhighlight %} |
| {% include code/end.html %} |
| |
| {% include code/start.html id="get1" type="async" %} |
| {% highlight js %} |
| try { |
| var doc = await db.get('mydoc'); |
| } catch (err) { |
| console.log(err); |
| } |
| {% endhighlight %} |
| {% include code/end.html %} |
| |
| {% include code/start.html id="get1" type="promise" %} |
| {% highlight js %} |
| db.get('mydoc').then(function (doc) { |
| // handle doc |
| }).catch(function (err) { |
| console.log(err); |
| }); |
| {% endhighlight %} |
| {% include code/end.html %} |
| |
| #### Example Response: |
| {% highlight js %} |
| { |
| "_id": "mydoc", |
| "_rev": "1-A6157A5EA545C99B00FF904EEF05FD9F" |
| "title": "Rock and Roll Heart", |
| } |
| {% endhighlight %} |
| |
| The response contains the document as it is stored in the database, along with its |
| `_id` and `_rev`. |