pouchdb.find 不是函数

pouchdb.find is not a function

我有一个使用 create-react-app 创建的 React 应用程序。所以使用与 create-react-app 捆绑在一起的 webpack。 我必须使用 Pouchdb 的 find(),这是我做不到的。其他 Pouch 功能运行良好,但 find 插件未附加到 Pouch。

import PouchDB from 'pouchdb';
PouchDB.plugin(require('pouchdb-find'));

package.json 中的版本:

"pouchdb": "^6.4.1",
"pouchdb-browser": "^6.4.1",
"pouchdb-find": "^6.4.1"

有没有人知道如何解决这个问题。 提前致谢。

我找到了解决这个问题的方法。希望对其他人有帮助

import PouchDB from 'pouchdb';
import PouchdbFind from 'pouchdb-find';

export class PouchService {
    constructor() {
        PouchDB.plugin(PouchdbFind);
    }
}

在此之后,find() 被包含在 PouchDB 对象中。

补充 Yash Kochar 的回答

import PouchDB from "pouchdb";
import PouchdbFind from 'pouchdb-find';

export default class Model{
    constructor(){
        PouchDB.plugin(PouchdbFind);
        this.db = new PouchDB('todos');
   }

   search(){
     this.db.find({
        selector: {name: 'MARIO'},
     }).then(function (result) {
     }).catch(function (err) {
       console.log(err);
     });
   }
}

注意:不要忘记在

之前安装:"npm install pouchdb" 和 "npm install pouchdb-find"

由于我们正在进行 PouchDB 初始化和配置,我已将代码放在它自己的 db.ts 文件中:

const PouchDB = require('pouchdb');

PouchDB.plugin(require('pouchdb-find'));

export const db = new PouchDB('my-db');

此外,我正在使用节点 require 语法。

效果很好,可以重复使用。

参考文献:

  1. https://pouchdb.com/guides/setup-pouchdb.html#typescript
  2. https://pouchdb.com/guides/mango-queries.html
import PouchDB from 'pouchdb-browser';
import PouchDBFind from 'pouchdb-find';
PouchDB.plugin(PouchDBFind);



let db = new PouchDB('employees');
db.find({
            
  selector: {age: 18}
}).then(doc => {
  console.log(doc) //queries the documents
});

请务必先 npm i pouchdb-browser pouchdb-find --save