pouchdb.find不是一个函数

问题描述 投票:2回答:2

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

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

reactjs webpack pouchdb create-react-app
2个回答
3
投票

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

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

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

在此之后,find()包含在PouchDB对象中。


0
投票

补充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”之前


0
投票

由于我们正在进行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
© www.soinside.com 2019 - 2024. All rights reserved.