Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class constructors cannot be invoked without 'new'

Just upgraded to node 4.1.2 and using Mongorito (which uses ES6) to access Mongo and I'm getting this:

Model file:

var Mongorito = require('mongorito');
var Model = Mongorito.Model;
var config = require('../config/config');
Mongorito.connect(config.mongo.url);

class Listing extends Model {}

module.exports = Listing;

And I'm including it:

var Listing = require('../models/listing');
var listing = yield Listing.where('cacheKey', key).findOne();
TypeError: Class constructors cannot be invoked without 'new'
      at Listing.Model (/node_modules/mongorito/lib/mongorito.js:140:15)
      at new Listing (/models/listing.js:7:14)
      at Query.find (/node_modules/mongorito/lib/query.js:355:21)
      at [object Generator].next (native)
      at onFulfilled (/node_modules/koa/node_modules/co/index.js:65:19)
      at run (/node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/es6.promise.js:89:39)
      at /node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/es6.promise.js:100:28
      at flush (/node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/$.microtask.js:17:13)
      at doNTCallback0 (node.js:408:9)
      at process._tickCallback (node.js:337:13)
like image 212
jabbermonkey Avatar asked Oct 07 '15 00:10

jabbermonkey


1 Answers

This is because Babel's transpiled ES6 classes cannot be used to extend a real ES6 class. If you want to use mongorito, you would have to blacklist Babel's es6.classes transform so that your Listing class was also a native ES6 class.

like image 173
loganfsmyth Avatar answered Nov 15 '22 22:11

loganfsmyth