Nodejs上的Javascript ES6:TypeError:对象不是构造函数 [英] Javascript ES6 on Nodejs : TypeError: object is not a constructor

查看:68
本文介绍了Nodejs上的Javascript ES6:TypeError:对象不是构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将此示例类 sync.js 作为模块放置在项目的某个位置.

I have this sample class sync.js as a module somewhere on my project.

'use strict';

export default class Sync{

    constructor(dbConnection){
        this.dbConnection = dbConnection;
    }

    test(){
        return "This is a test " + this.dbConnection;
    }
}

然后在我的控制器上的某个地方,我将该类用作:

Then somewhere on my controller I am using this class as :

'use strict';

import Sync from '../../path/to/module'; // <-- works fine

const sync = new Sync('CONNECTION!'); // <-- meh

console.log(sync.test());

我希望这样的事情会记录在控制台This is a test CONNECTION!上.但是相反,我遇到了这个错误. TypeError: object is not a constructor

I was expecting something like this to be logged on the console This is a test CONNECTION!. But instead I am getting this error. TypeError: object is not a constructor

我做错了什么?

顺便说一句,如果我删除了行const sync = new Sync('CONNECTION!');并将console.log()更改为console.log(Sync.test());,则会输出输出This is a test undefined,这与我的预期相符.但是我的身份有何不妥呢?

By the way if I removed the line const sync = new Sync('CONNECTION!'); and changed console.log() to console.log(Sync.test()); the output This is a test undefined is printed which is kind of what I expected. But what's wrong with my instatiation?

WTF?

修改

我认为我是根据 @JLRishe

Guys I think I found the problem, based on @JLRishe and rem035 pointed out, it was returning the instance of the class not the class itself. In fact there is an index.js that imports the './sync' js file and exporting is as export default new Sync();. Here's the whole index.js.

'use strict';

import Sync from './sync';

export default new Sync(); // <-- potential prodigal code

模块树如下所示.

module
  |
  |_ lib
  |  |_ index.js // this is the index.js I am talking about
  |  |_ sync.js
  |
  |_ index.js // the entry point, contains just `module.exports = require('./lib');`

现在.如何不执行new导出export default new Sync();?

Now. How do I export export default new Sync(); without doing new?

推荐答案

编辑2

如何导出导出默认的新Sync();不做新事?

How do I export export default new Sync(); without doing new?

只需从module/lib/index.js删除new关键字:

import Sync from './sync';

export default Sync;

或直接从module/lib/sync.js

编辑1

根据您所说的记录,

Sync { dbConnection: undefined }

似乎您的导入返回的是类的实例(这是一个对象),而不是类定义本身.

it seems like your import is returning an instance of the class (which is an object), rather than the class definition itself.

所以console.log(new Sync())会返回您所说的话,

So console.log(new Sync()) would return what you are saying,

class Sync {

  constructor(dbConnection) {
    this.dbConnection = dbConnection;
  }

  test() {
    return "This is a test " + this.dbConnection;
  }
}

console.log(new Sync());

不是console.log(Sync)

class Sync {

  constructor(dbConnection) {
    this.dbConnection = dbConnection;
  }

  test() {
    return "This is a test " + this.dbConnection;
  }
}

console.log(Sync);

确定要在导出之前未在任何地方致电new Sync吗?

Are you sure you aren't calling new Sync anywhere prior to exporting?

初始答案

有问题的代码可以正常工作:

The code in question works fine:

'use strict';

class Sync {

  constructor(dbConnection) {
    this.dbConnection = dbConnection;
  }

  test() {
    return "This is a test " + this.dbConnection;
  }
}

const sync = new Sync('CONNECTION!');

console.log(sync.test());

根据您的错误:

TypeError: object is not a constructor

您的import没有返回您认为正在返回的内容,并且您正在尝试new无法实例化的内容.

Your import is not returning what you think it's returning and you are trying to new something that cannot be instantiated.

您的import路径很可能是错误的.

Most likely your import path is wrong.

这篇关于Nodejs上的Javascript ES6:TypeError:对象不是构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆