[英]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
Add to your source file添加到您的源文件
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();
To instantiate the database实例化数据库
const db = new pouchDB('MyDB'); const db = new pouchDB('MyDB');
对于没有构造函数使用 .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 作为函数并且不使用构造函数:-
const PouchDB = require('pouchdb').default;
将 pouch DB 导入为:- const PouchDB = require('pouchdb').default;
in your TS file.在您的 TS 文件中。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 的类型支持。"types": ["node"]
in compiler object.然后在tsconfig.app.json文件中添加"types": ["node"]
在编译器对象中。 This step is used to support node types example 'require'.此步骤用于支持节点类型示例“require”。const db = PouchDB('MyDB');
要实例化数据库,请将其添加到您的 TS 文件中const db = PouchDB('MyDB');
To use pouch DB with the constructor:-将 pouch DB 与构造函数一起使用:-
import PouchDB from 'pouchdb'
in your TS file.将 pouch DB 导入为:- 从 TS 文件中import PouchDB from 'pouchdb'
。const db = new PouchDB('MyDb');
要实例化数据库,请将其添加到您的 TS 文件const db = new PouchDB('MyDb');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.