【问题标题】:pouchdb.find is not a functionpouchdb.find 不是函数
【发布时间】:2021-10-14 01:07:51
【问题描述】:

我有一个使用 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"

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

【问题讨论】:

    标签: reactjs webpack pouchdb create-react-app


    【解决方案1】:

    我找到了解决这个问题的方法。希望它会帮助别人

    import PouchDB from 'pouchdb';
    import PouchdbFind from 'pouchdb-find';
    
    export class PouchService {
        constructor() {
            PouchDB.plugin(PouchdbFind);
        }
    }

    在这个 find() 被包含在 PouchDB 对象中之后。

    【讨论】:

      【解决方案2】:

      补充 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);
           });
         }
      }
      

      注意:不要忘记 install: "npm install pouchdb" 和 "npm install pouchdb-find" 之前

      【讨论】:

        【解决方案3】:
        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

        【讨论】:

          【解决方案4】:

          由于我们正在进行 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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-05-06
            • 2019-12-20
            • 1970-01-01
            • 2021-12-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-02-21
            相关资源
            最近更新 更多