pouchdb.find is not a function

Deadly 提交于 2019-12-13 13:53:53

问题


I have an react app created using create-react-app. So using webpack which is bundled with create-react-app. I have to use find() of Pouchdb, which I am unable to do. Other Pouch functionality is working fine but find plugin is not getting attached to Pouch.

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

Versions in package.json :

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

Do anyone have Idea how to fix this. Thanks in advance.


回答1:


I found the way to solve this problem. Hope it will help others

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

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

After this find() gets included in PouchDB objects.




回答2:


Complementing the answer of 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);
     });
   }
}

NOTE: don't forget of install: "npm install pouchdb" and "npm install pouchdb-find" before




回答3:


Since we are doing PouchDB initialization and configuration, I've put the code in its own db.ts file:

const PouchDB = require('pouchdb');

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

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

Also, I am using node require syntax.

It works very well and is ready to be reused.

References:

  1. https://pouchdb.com/guides/setup-pouchdb.html#typescript
  2. https://pouchdb.com/guides/mango-queries.html


来源:https://stackoverflow.com/questions/48186831/pouchdb-find-is-not-a-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!