Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
555 views
in Technique[技术] by (71.8m points)

javascript - Node.js v6.2.0 class extends is not a function error?

So I'm trying to extend a class in node js and the compiler keeps returning the following error:

TypeError: Class extends value #<Object> is not a function or null

I checked that I was exporting the class correctly and I am, any ideas? I'll post my code below:

/handler/venue.js:

var VenueViews = require('../views/venue'); // If I remove this the error will dissapear (as expected)
class Venue {
  constructor(data) {
    this.setDataHere = data;
  }

  main () {
   var View = new VenueViews(); // This doesn't run
  }


}

module.exports = Venue;

/views/venue.js:

var Venue = require('../handlers/venue');
console.log  (Venue) // This returns {} ???

class VenueViews extends Venue {
  constructor() {
    super();
  }
}

module.exports = VenueViews;

I know that node supports these es6 features, so I'm unsure why they aren't working?

Edit:

I'm not sure if this is suppose to happen but, when I log my Venue require it returns an empty object {}.

console.log  (Venue) // This returns {} ???
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

So it turns out I had a circular reference in my code, where I was importing the class that was extending, into the class that itself was extending (tongue twister :P).

The obvious fix was to simply remove the extends reference and find another way of doing what I was trying to achieve. In my case it was passing the Venue class properties down into the VenueViews constructor.

E.g var x = VenueViews(this)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...