简体   繁体   English

TypeError:Name.default不是构造函数

[英]TypeError: Name.default is not a constructor

I am writing a unit test for a service class called TaskService . 我正在为称为TaskService的服务类编写单元测试。 The unit test injects a TaskService instance, whose constructor goes ahead to create a DB connection to NeDB. 单元测试将注入TaskService实例,该实例的构造函数将继续创建与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 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'}); 测试跑步者抱怨this.tasks = new Datastore({filename: 'db/tasks.json'}); line. 线。

Here is the index.js file for the NeDB package. 这是NeDB软件包的index.js文件。

index.js 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. 就我而言,我试图在Angular 4应用程序中使用非Typescript npm软件包。 the import statements for non typescript packages are always tricky to get right. 非打字稿包的导入语句总是很棘手。

app.module.ts 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 TypeError:xxx不是构造函数

I cannot apply the solution in this question because NeDB is an NPM package, not my own class: Error: *.default is not a constructor 我无法在此问题中应用解决方案,因为NeDB是NPM软件包,而不是我自己的类: 错误:* .default不是构造函数

I finished typing the question and then stumbled across this github issue: https://github.com/goatslacker/alt/issues/681 我完成了问题的输入,然后偶然发现了这个github问题: https : //github.com/goatslacker/alt/issues/681

One comment states the following: 一条评论指出:

This works: 这有效:

import Alt from 'alt/lib' 从'alt / lib'导入Alt

var alt = new Alt(); var alt = new Alt();

module.exports = 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'); 就我而言,我没有使用import Datastore from 'nedb'import Datastore from 'nedb' ,而是删除了导入,而是将以下内容添加到了我的构造函数中: var Datastore = require('nedb');

This solved the issue. 这解决了问题。 Had to add the Datastore variable to my beforeEach function in my unit test as well. 也必须在单元测试中将Datastore变量添加到我的beforeEach函数中。

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

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