简体   繁体   中英

TypeError: Name.default is not a constructor

I am writing a unit test for a service class called TaskService . The unit test injects a TaskService instance, whose constructor goes ahead to create a DB connection to NeDB. (i know I should mock this connection but I need to get the unit test working before I can optimise it).

task.service.ts

import {Injectable, OnDestroy} from '@angular/core';
import {Task} from "./task.model";
import Datastore from 'nedb';

@Injectable()
export class TaskService implements OnDestroy {
  private tasks: any;

  constructor(private _logger: Logger) {
    this._logger.log("Task Service Constructor");

    this.tasks = new Datastore({filename: 'db/tasks.json'});


  }

Error Message

  TypeError: nedb_1.default is not a constructor at new TaskService I:/Projects/taskmelater/src/app/tasks/shared/task.service.ts:22:18) at _createClass I:/Projects/taskmelater/node_modules/@angular/core/@angular/core.es5.js:9529:1) at _createProviderInstance$1 I:/Projects/taskmelater/node_modules/@angular/core/@angular/core.es5.js:9503:1) at resolveNgModuleDep I:/Projects/taskmelater/node_modules/@angular/core/@angular/core.es5.js:9488:1) at NgModuleRef_.webpackJsonp.../../../core/@angular/core.es5.js.NgModuleRef_.get I:/Projects/taskmelater/node_modules/@angular/core/@angular/core.es5.js:10562:1) at TestBed.webpackJsonp.../../../core/@angular/core/testing.es5.js.TestBed.get I:/Projects/taskmelater/node_modules/@angular/core/@angular/core/testing.es5.js:819:1) at Function.webpackJsonp.../../../core/@angular/core/testing.es5.js.TestBed.get I:/Projects/taskmelater/node_modules/@angular/core/@angular/core/testing.es5.js:656:1) at UserContext.<anonymous> I:/Projects/taskmelater/src/app/tasks/shared/task.service.spec.ts:20:24) at ZoneDelegate.webpackJsonp.../../../../zone.js/dist/zone.js.ZoneDelegate.invoke I:/Projects/taskmelater/node_modules/zone.js/dist/zone.js:392:1) at ProxyZoneSpec.webpackJsonp.../../../../zone.js/dist/proxy.js.ProxyZoneSpec.onInvoke I:/Projects/taskmelater/node_modules/zone.js/dist/proxy.js:79:1) 

The test runner is complaining about the this.tasks = new Datastore({filename: 'db/tasks.json'}); line.

Here is the index.js file for the NeDB package.

index.js

 var Datastore = require('./lib/datastore'); module.exports = Datastore; 
I read through a ton of similar questions but they are about a custom class that causes this error. In my case I am trying to use a non Typescript npm package in my Angular 4 application. the import statements for non typescript packages are always tricky to get right.

app.module.ts

 @NgModule({ declarations: [ AppComponent, StarterComponent, StarterHeaderComponent, StarterLeftSideComponent, StarterContentComponent, StarterFooterComponent, StarterControlSidebarComponent, TasksComponent, TaskComponent, TaskFormComponent, TaskService ], imports: [ BrowserModule, AppRoutingModule, AdminModule, AgGridModule, RouterModule, FormsModule ], providers: [TaskService, Logger], bootstrap: [AppComponent, TaskService, Logger] }) export class AppModule {} 

Questions consulted:

TypeError: xxx is not a constructor

I cannot apply the solution in this question because NeDB is an NPM package, not my own class: Error: *.default is not a constructor

I finished typing the question and then stumbled across this github issue: https://github.com/goatslacker/alt/issues/681

One comment states the following:

This works:

import Alt from 'alt/lib'

var alt = new Alt();

module.exports = alt;

In my case, rather than importing NeDB using import Datastore from 'nedb' , I removed the import and instead added the following to my constructor: var Datastore = require('nedb');

This solved the issue. Had to add the Datastore variable to my beforeEach function in my unit test as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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