pouchdb.find is not a function
Asked Answered
N

4

5

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.

Narcho answered 10/1, 2018 at 11:48 Comment(0)
N
9

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.

Narcho answered 11/1, 2018 at 5:44 Comment(0)
C
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
});

Make sure you do npm i pouchdb-browser pouchdb-find --save first.

Cockneyism answered 13/10, 2021 at 17:7 Comment(0)
R
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

Recognize answered 18/2, 2019 at 14:5 Comment(0)
T
1

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
Transmittal answered 28/8, 2019 at 18:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.