admin管理员组

文章数量:1277875

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());

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

What did I do wrong?

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?

Edit

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

The module tree looks like this.

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');`

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

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());

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

What did I do wrong?

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?

Edit

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

The module tree looks like this.

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');`

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

Share Improve this question edited May 23, 2017 at 12:10 CommunityBot 11 silver badge asked Jul 27, 2016 at 18:20 jofftiquezjofftiquez 7,70810 gold badges70 silver badges122 bronze badges 14
  • This works for fine for me – nem035 Commented Jul 27, 2016 at 18:26
  • WTH? Really? @nem035 that's weird. :( – jofftiquez Commented Jul 27, 2016 at 18:26
  • 1 Are you sure you don't have a new accidentally in your default export? The code you've shown us does export the class, not an instance. – Bergi Commented Jul 27, 2016 at 18:32
  • 1 @TheGreenFoxx It seems pretty clear your Sync variable is an instance of the Sync class rather than the class itself. Couldn't say why though. Are you showing us the full contents, unmodified, of your sync.js file? – JLRishe Commented Jul 27, 2016 at 18:32
  • 1 @TheGreenFoxx But was it the whole module code? Are you sure you're not doing Sync = new Sync somewhere in there? – Bergi Commented Jul 27, 2016 at 18:39
 |  Show 9 more ments

2 Answers 2

Reset to default 4

EDIT 2

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

Just remove the new keyword from module/lib/index.js:

import Sync from './sync';

export default Sync;

Or directly import from module/lib/sync.js


EDIT 1

Based on the what you are saying is logged,

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.

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());

not console.log(Sync)

class Sync {

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

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

console.log(Sync);

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


Initial answer

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());

Based on your error:

TypeError: object is not a constructor

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

Most likely your import path is wrong.

Since this is the top result on google:

If you are using require() statements in Node to import classes and introduce a circular dependency, you will suddenly see this error popping up because require() is returning {} instead of the class.

本文标签: nodejsJavascript ES6 on NodejsTypeError object is not a constructorStack Overflow