最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uncaught TypeError: PouchDB is not a constructor - Stack Overflow

programmeradmin4浏览0评论

I'm struggling to understand this error: 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. I've tested on another simple .js file and works, but on my main app it isn't. Still, I don't understand why it wouldn't work. The pouch documentation is quite clear .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'm struggling to understand this error: 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. I've tested on another simple .js file and works, but on my main app it isn't. Still, I don't understand why it wouldn't work. The pouch documentation is quite clear https://pouchdb./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!

Share Improve this question edited Oct 31, 2018 at 16:13 mihai 38.6k11 gold badges62 silver badges89 bronze badges asked Oct 18, 2018 at 14:06 Matias SalimbeneMatias Salimbene 6058 silver badges21 bronze badges 2
  • this is probably electron specific, it works fine in vanilla node.js – mihai Commented Oct 31, 2018 at 16:15
  • There's a number of possible fixes detailed at github./pouchdb/pouchdb/issues/6692 – Sam Hanley Commented Oct 31, 2018 at 16:16
Add a ment  | 

4 Answers 4

Reset to default 6

I found the solution here

To use PouchDB with Typescript:

Install PouchDB

  1. npm install pouchdb-browser
  2. 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 = PouchDB.default.defaults();

  2. To instantiate the database

    const db = new pouchDB('MyDB');

The rest is all here.

For without constructor use .default

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

change:

var PouchDB = require('pouchdb');

to:

import PouchDB from 'pouchdb'

This error is ing because the piler is not able to get the pouch DB constructor from the package.

To use pouch DB as a function & without the constructor:-

  • Import pouch DB as :- const PouchDB = require('pouchdb').default; in your TS file.
  • Then run: npm i --save-dev @types/node mand to install the type support for node from the npm registry.
  • Then in tsconfig.app.json file add "types": ["node"] in piler object. This step is used to support node types example 'require'.
  • To instantiate the database add this in your TS file const db = PouchDB('MyDB');
  • Then serve the project

To use pouch DB with the constructor:-

  • Import pouch DB as:- import PouchDB from 'pouchdb' in your TS file.
  • To instantiate the database add this to your TS file const db = new PouchDB('MyDb');
  • Then serve the project.
发布评论

评论列表(0)

  1. 暂无评论