问题
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:
- https://pouchdb.com/guides/setup-pouchdb.html#typescript
- https://pouchdb.com/guides/mango-queries.html
来源:https://stackoverflow.com/questions/48186831/pouchdb-find-is-not-a-function