Class constructors cannot be called without "new"

Just upgraded to node 4.1.2 and using Mongorito (which uses ES6) to access Mongo, and I get the following:

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 turn it on:

 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) 
+5
source share
3 answers

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

+11
source

It turns out that if you use the es2015 preset on the library side, the user can extend the classes defined on them.

.babelrc

 { "presets": ["es2015"] } 

I did not test on mongorito , but I had the same problem that extended to a third-party class and used this preset for me.

+1
source

Overloaded classes cause a problem.
If you use the env preset, you can exclude the class plugin as follows:

  presets: [ ["env", { exclude: ["transform-es2015-classes"] }] ] 
+1
source

Source: https://habr.com/ru/post/1233100/


All Articles