1

I'm asking this mostly as a sanity check, I'm unsure if I have made a mistake somewhere or if this fundamentally does not work.

I am converting some Java to Nodejs, A class called Site populates itself from a database via the dataFunctions.getSiteInfo(id) function. It also stores a list of Page instances.

The issue is that dataFunctions is returning as undefined when an instance of Site is created.

Have I misunderstood how classes and modules work in this case? Is it possible for instances of this class to access the dataFunctions module? Likewise is it possible for the Site class to reference the Page class as well? Or have I made some other silly mistake?

Site.js

let Page = require('./Page.js');
let dataFunctions = require('../DataFunctions.js');

module.exports = class Site {
    constructor(id) {
        this.id = id;
        this.pages = [];

        console.log(dataFunctions);
        this.siteInfo = dataFunctions.getSiteInfo(id);

DataFunctions.js

let Site = require('./classes/Site.js');

function makeASite() {
    let id = 2;
    let site = new Site(id);
}

function getSiteInfo(id) {
    etc etc
}

module.exports = {
    getSiteInfo: function (id) {
        return getSiteInfo(id);
    },
};
Daniel
  • 357
  • 1
  • 2
  • 12
  • 1
    pls post your project structure – BeeFriedman Sep 30 '21 at 17:00
  • 2
    You have a circular require loop. Can't do that. Site.js requires DataFunctions.js and DataFunctions.js requires Site.js. The usual solution here is to break out the common code that both modules want to use into a third module that each can include rather than including each other. – jfriend00 Sep 30 '21 at 18:02
  • Replace that export by `module.exports.getSiteInfo = getSiteInfo` and [it will work](https://stackoverflow.com/q/10869276/1048572) – Bergi Nov 28 '21 at 21:47

1 Answers1

1

Based on the code provided, I believe you forgot to create a new dataFunctions instance.

const df = new dataFunctions();
console.log(df);

If dataFunctions is providing static functions, make sure that the getSiteInfo method is static.

Otherwise, additional details may be needed.

Edit

It looked like the project structure and the require paths were wonky, but it's hard to tell.

When I run the code provided using the project structure inferred by the require statements, I'm not able to reproduce your issue.

However, one problem that I do see is that you're requiring the Site.js module from the DataFunctions.js module and requiring the DataFunctions.js module from the Site.js module, creating circular dependencies. Try eliminating these by moving makeASite to the Site.js module, for instance.

phentnil
  • 2,195
  • 2
  • 14
  • 22
  • 1
    Hold on, I thought that classes in JS were just fancy JS objects. You don't need to create instances of functions in JS normally. – Daniel Sep 30 '21 at 17:04
  • How is the `dataFunctions` class declared? I just tested static vs instantiated and it worked fine when i declared `static getSiteInfo` but gave me an error that `getSiteInfo` was not a function without the static declaration. If you're getting an undefined, it might be a different error altogether. – phentnil Sep 30 '21 at 17:10
  • 1
    I updated the post with more info about this, dataFunctions is not a class its a module, sorry should have been more clear. – Daniel Sep 30 '21 at 17:11
  • 1
    @Nonnisi Classes in js are just functions. Objects in JS are objects. The difference with Java is that you can't create object literal in Java (though you can create a string literal, eg: `"hello"`, number literal etc.). Though Java does have something similar: anonymous classes. – slebetman Sep 30 '21 at 17:45
  • @phentnil I agree with your comment on the circular loop, the issue is that the dataFunctions is actually a mySQL connector and manages the connection, so it needs to be referenced by anything making a database call. (The database issue is still a mess due to JS callbacks and promises needing to be added). Im starting to think this code just cant be converted, I might need to totally rethink the project structure. – Daniel Sep 30 '21 at 17:52