PouchDB教程

PouchDB 检索文档

db.get()方法用于读取或检索在数据库中创建的文档。此方法还接受文档ID和可选的回调函数。
语法:
db.get(document, callback)

PouchDB阅读文档示例

//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);
   }
});
将以上代码保存在名为" PouchDB_Examples"的文件夹中的名为" Read_Document.js"的文件中。打开命令提示符,并使用node执行JavaScript文件:
node Read_Document.js
它将读取存储在PouchDB服务器上" Second_Database"中的文档。
{ name: 'Ajeet',
  age: 28,
  designation: 'Developer',
  _id: '001',
  _rev: '1-99a7a80ec2a74959885037a16d57924f' }
PouchDB阅读文档1

从远程数据库中读取文档

您可以从远程数据库(CouchDB)中读取或检索文档。为此,您必须在CouchDB中传递数据库的路径,该路径包含要读取的文档而不是数据库名称。

示例

CouchDB服务器中有一个名为"员工"的数据库。
PouchDB阅读文档2
通过单击"员工",您可以看到一个文档:
PouchDB阅读文档3
让我们获取该文档的数据:
//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);
   }
});
将以上代码保存在名为" PouchDB_Examples"的文件夹中的名为" Read_Remote_Document.js"的文件中。打开命令提示符,并使用node执行JavaScript文件:
node Read_Remote_Document.js
它将读取存储在CouchDB服务器上"员工"数据库中的文档。
输出:
{ _id: '001',
  _rev: '3-276c137672ad71f53b681feda67e65b1',
  name: 'Ajeet',
  age: 28,
  designation: 'Developer' }
PouchDB阅读文档4
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4