简体   繁体   English

未捕获的 TypeError:PouchDB 不是构造函数

[英]Uncaught TypeError: PouchDB is not a constructor

I'm struggling to understand this error: Uncaught TypeError: PouchDB is not a constructor我很难理解这个错误: Uncaught TypeError: PouchDB is not a constructor

The code is as follows:代码如下:

var PouchDB = require("pouchdb");
var db = new PouchDB("scr");

I've read about how it may be related to types and that adding:我已经阅读了它与类型的关系以及添加:

 "@types/node": "^10.12.0",
 "@types/pouchdb": "^6.3.2",

to my package.json should help, but it isn't.对我的 package.json 应该有帮助,但事实并非如此。 I've tested on another simple .js file and works, but on my main app it isn't.我已经在另一个简单的 .js 文件上进行了测试并且可以工作,但在我的主应用程序上却不是。 Still, I don't understand why it wouldn't work.不过,我不明白为什么它不起作用。 The pouch documentation is quite clear https://pouchdb.com/api.html#create_document .邮袋文档非常​​清楚https://pouchdb.com/api.html#create_document

I should mention I'm running this on the context of an electron app, not in the browser.我应该提到我在电子应用程序的上下文中运行它,而不是在浏览器中。

I'm baffled at this point, any help would be greatly appreciated.我在这一点上感到困惑,任何帮助将不胜感激。 Cheers!干杯!

I found the solution here我在这里找到了解决方案

To use PouchDB with Typescript:将 PouchDB 与 Typescript 一起使用:

Install PouchDB安装 PouchDB

  1. npm install pouchdb-browser npm 安装 pouchdb 浏览器
  2. npm install --save-dev @types/pouchdb-browser npm install --save-dev @types/pouchdb-browser

Add to your source file添加到您的源文件

  1. At the top in your "imports" section在“导入”部分的顶部

    const PouchDB = require('pouchdb-browser'); const PouchDB = require('pouchdb-browser');

    const pouchDB = PouchDB.default.defaults(); const pouchDB = PouchDB.default.defaults();

  2. To instantiate the database实例化数据库

    const db = new pouchDB('MyDB'); const db = new pouchDB('MyDB');

The rest is all here .剩下的都在这里

对于没有构造函数使用 .default

const PouchDB = require('pouchdb').default;

change : var PouchDB = require('pouchdb');更改: var PouchDB = require('pouchdb');

to : import PouchDB from 'pouchdb' to:从“pouchdb”导入 PouchDB

This error is coming because the compiler is not able to get the pouch DB constructor from the package.出现此错误是因为编译器无法从包中获取 pouch DB 构造函数。

To use pouch DB as a function & without the constructor:-要使用 pouch DB 作为函数并且不使用构造函数:-

  • Import pouch DB as :- const PouchDB = require('pouchdb').default;将 pouch DB 导入为:- const PouchDB = require('pouchdb').default; in your TS file.在您的 TS 文件中。
  • Then run: npm i --save-dev @types/node command to install the type support for node from the npm registry.然后运行: npm i --save-dev @types/node命令从 npm 注册表安装 node 的类型支持。
  • Then in tsconfig.app.json file add "types": ["node"] in compiler object.然后在tsconfig.app.json文件中添加"types": ["node"]在编译器对象中。 This step is used to support node types example 'require'.此步骤用于支持节点类型示例“require”。
  • To instantiate the database add this in your TS file const db = PouchDB('MyDB');要实例化数据库,请将其添加到您的 TS 文件中const db = PouchDB('MyDB');
  • Then serve the project然后服务项目

To use pouch DB with the constructor:-将 pouch DB 与构造函数一起使用:-

  • Import pouch DB as:- import PouchDB from 'pouchdb' in your TS file.将 pouch DB 导入为:- 从 TS 文件中import PouchDB from 'pouchdb'
  • To instantiate the database add this to your TS file const db = new PouchDB('MyDb');要实例化数据库,请将其添加到您的 TS 文件const db = new PouchDB('MyDb');
  • Then serve the project.然后为项目服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM