1

I have this class which we're going to call Connection, and there's a function in this class named Disconnect which returns a class named PartialConnection. There are some arguments for both classes, but that shouldn't be taken into effect because it's quite complicated for such an example.

This is the class structure I'm working with as a sketch:

const BaseConnection    = require("./BaseConnection.js");
const PartialConnection = require("./PartialConnection.js");

class Connection extends BaseConnection { // BaseConnection is also a class with standard variables every type of connection should have. PartialConnection also extends from this.
  constructor () {
    ...
  }

  Disconnect() {
    return new PartialConnection();
  }
}

Before you all ask, the path to ./PartialConnection.js is correct and it's set with module.exports = PartialConnection; with PartialConnection being the class name.

File PartialConnection.js

const Connection = require("./Connection");
const BaseCon    = require("./BaseConnection");

class PartialConnection extends BaseCon {
  constructor () {
    ...
  }
}

module.exports = PartialConnection;

Although, I must say that the colouring of the module.exports = PartialConnection is off like shown here: https://i.stack.imgur.com/I9hpG.jpg - I also should say that, when executing the same function but for the PartialConnection to Connection, it just works fine. It has something to do with circular references.

When I create a new instance of Connection and run the Disconnect function, it returns the following error:

/Users/---/Desktop/Projects/QDB/lib/Connections/Connection.js:87
        return new PartialConnection();
               ^

TypeError: PartialConnection is not a constructor
    at Connection.Disconnect (/Users/---/Desktop/Projects/QDB/lib/Connections/Connection.js:87:16)
    at process.<anonymous> (/Users/---/Desktop/Projects/QDB/lib/Connections/Connection.js:76:75)
    at process.emit (events.js:219:5)

(I blanked out my name for privacy.)

As you can see, it seems like I can't initiate and return a new class of some sort. This used to work about a few weeks ago, but now it doesn't.

Version;

$ node -v
v13.3.0
$ npm -v
6.14.2

And for clarification - I'd like it to almost-literally terminate the current class and return a new class that doesn't allow you to make adjustments to the Connection class.

If you have any solutions or if you can help in any way, it's much appreciated!

Smally
  • 1,556
  • 1
  • 8
  • 27
  • 2
    The first thing to do when you get that error is `console.log(PartialConnection);` right under your `const PartialConnection = require('...');` to see what you're getting instead – blex Mar 13 '20 at 17:54
  • @blex It returns an empty object. Huh. Does this mean that `PartialConnection` isn't cached or so? – Smally Mar 13 '20 at 17:56
  • It means that the `PartialConnection.js` file does not export what you think it does. Could you share a partial version of that file? – blex Mar 13 '20 at 17:57
  • 1
    Sure. I'll edit the question in a bit. – Smally Mar 13 '20 at 17:58
  • 2
    This is a circular dependency issue - partial connection requires connection and connection requires partial connection. Use functions to return the classes when called and you should be fine. – Adam Jenkins Mar 13 '20 at 18:06
  • Not flagging as duplicate since I'm not sure, but I agree with Adam. https://stackoverflow.com/questions/10869276/how-to-deal-with-cyclic-dependencies-in-node-js – blex Mar 13 '20 at 18:09
  • I'd reckon. Is there a way I can, for both classes, almost-literally 'switch' to them when a function is called? If the `Disconnect()` function is called, I want to return the partial connection class. Is that possible somehow? – Smally Mar 13 '20 at 18:09
  • put both classes in the same file – Max Mar 13 '20 at 19:10

1 Answers1

0

Alright, so I have experimented a bit more and found out that, if you require the class within the function instead of at the beginning of the file, it caches instantly and you're able to call it instead of having it return an empty object.

const BaseConnection = require("./BaseConnection");

class Connection extends BaseConnection {
  constructor () {
    ...
  }

  Disconnect() {
    const PartialConnection = require("./PartialConnection");
    return new PartialConnection();
  }
}

If you have any other answers, later on, feel free to share!

Smally
  • 1,556
  • 1
  • 8
  • 27