PouchDB Read Document
PouchDB Read Document
- db.get() - This method is used to retrieve or read the document created in a database. This method takes the document id and callback function as a parameter.
Syntax
db.get(document, callback)
Example:
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('Second_Database');
//Reading the contents of a Document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
- Save the file name as "Read_Document.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file:
node Read_Document.js
- It'll examined the document put away in "Second_Database" on PouchDB Server.
Output:
{ name: 'Admin',
age: 28,
designation: 'Developer',
_id: '001',
_rev: '1-99a7a80ec2a74959885037a16d57924f' }
Read Also
![PouchDB Read/Retrieve Document](https://cdn.wikitechy.com/tutorial/pouchdb/read-document-1.png)
PouchDB Read/Retrieve Document
Read a Document from Remote Database
You can also read or retrieve a document from a remotely stored database(CouchDB). You just pass the path of the database where you want to read documents in CouchDB, instead of database name.
Example
- There is a database named "employees" in the CouchDB Server.
![PouchDB Read/Retrieve Document](https://cdn.wikitechy.com/tutorial/pouchdb/read-document-2.png)
PouchDB Read/Retrieve Document
- By clicking on "employees", you can see a document:
![PouchDB Read/Retrieve Document](https://cdn.wikitechy.com/tutorial/pouchdb/read-document-3.png)
PouchDB Read/Retrieve Document
- Let's fetch data of that document:
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('http://localhost:5984/employees');
//Reading the contents of a document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
- Save the file named as "Read_Remote_Document.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:
node Read_Remote_Document.js
- It will read the document stored in "employee" database on CouchDB Server.
Output:
{ _id: '001',
_rev: '3-276c137672ad71f53b681feda67e65b1',
name: 'Admin',
age: 28,
designation: 'Developer' }
![PouchDB Read/Retrieve Document](https://cdn.wikitechy.com/tutorial/pouchdb/read-document-4.png)
PouchDB Read/Retrieve Document