TypeError Database is Not a Constructor


#1

September 2, 2018 (Europe: Paris), September 2, 2018 (America: Los Angeles)

Hello,

I have been working on a project for a class and have been trying to import RPG/MUD elements into my Discord bot and it has been going pretty well. I had to change the require statements from relative paths to literal paths in order for Glitch to not throw errors but now I am running into something else that has me stumped.

To start here is the error:

Unable to load command adventure.js: TypeError: ItemDatabase is not a constructor

11:03 AM

at Object.<anonymous> (/app/Databases.js:11:16)

11:03 AM

at Module._compile (module.js:652:30)

11:03 AM

at Object.Module._extensions…js (module.js:663:10)

11:03 AM

at Module.load (module.js:565:32)

11:03 AM

at tryModuleLoad (module.js:505:12)

11:03 AM

at Function.Module._load (module.js:497:3)

11:03 AM

at Module.require (module.js:596:17)

11:03 AM

at require (internal/module.js:11:18)

11:03 AM

at Object.<anonymous> (/app/Train.js:3:30)

11:03 AM

at Module._compile (module.js:652:30)

To introduce the game elements, I have been using class constructors that look something like this

class EntityDatabase {

constructor() {
this.map = new SortedMap([], (a, b) => a > b);
}

add(entity) {
const id = this.findOpenId();
entity.id = id;
this.map.set(parseInt(id), entity);
//console.log(id);
}

get(id) {
return this.map.get(id);
}

findOpenId() {
let previousId = 0;
for (let key of this.map.keys()) {
if (key !== previousId + 1) break;
previousId = key;
}
return previousId + 1;
}

Just added a little bit of the code(the full code is quite long)

And then there are some that extend the class:

const path = require(‘path’);
const jsonfile = require(‘jsonfile’);
const EntityDatabase = require(__dirname +’/EntityDatabase’);
const Item = (__dirname +’/Item’);

//changed from path.join

const file = require(__dirname + ‘/gamedata/items.json’);

class ItemDatabase extends EntityDatabase {

constructor() {
super();
}

load() {
this.map.clear();
const dataArray = jsonfile.readFileSync(file);
dataArray.forEach(dataObject => {
const item = new Item();
item.load(dataObject);
this.add(item);
});
}

}

const ItemDatabase = ItemDatabase

module.exports = ItemDatabase;

It appears that **Not a Constructor ** error can be tied to a variety, but the first thing that I can think of is that I had I had to change the require statements from this:

const ItemDatabase = require(path.join(__dirname, ‘…’, ‘src’, ‘ItemDatabase’));

to this:

const ItemDatabase = require(__dirname, + ‘/ItemDatabase’);

in order for Glitch to find the module and perhaps this borked something up in the process.

Has anyone else run into any weirdness when dealing with class constructors? If so, how did you get past it? Any help would be appreciated. I’ve come so far on this project and it’s such a shame to get stuck at something like this.


#2

It would be useful to be able to see the code in context, and the file structure etc. Let us know the project name and we’ll take a look. If your project is private, either make it public temporarily or DM me a join link so we can view the code. Thanks.


#3

There were a few little typos, like extra commas or missing require statements presumably introduced when switching out path.join() e.g. require(__dirname, + ‘/ItemDatabase’). I’ve fixed those up and it’s progressed - it’s now stalled on a missing map data file but i’m sure you can take it from here.


#4

Thank you again for the help.