我有一个使用create-react-app创建的反应应用程序。所以使用与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
语法。
它工作得很好,可以重复使用。
参考文献: