Skip to content
This repository has been archived by the owner on Feb 3, 2020. It is now read-only.

Cannot read property 'insert' of undefined #5

Closed
varinyc opened this issue Mar 12, 2018 · 3 comments
Closed

Cannot read property 'insert' of undefined #5

varinyc opened this issue Mar 12, 2018 · 3 comments

Comments

@varinyc
Copy link

varinyc commented Mar 12, 2018

My app.js script always fails on the first run, when having a fresh rethink database. If I run my script once, then shut it down, then reload, everything seems to run just fine.

const Enmap = require('enmap');
const EnmapRethink = require('enmap-rethink');
var playerDB = [];

const playerAPI = new EnmapRethink({
  name: 'players',
  dbName: 'PLAYERSDB',
});

playerDB[0] = new Enmap({provider: playerAPI, persistent: true});

Then >

bot.on('ready', () => {
 createLibrary();
});

function createLibrary() {
  playerDB[0].set('foundPlayers', 0);
}

Even though i read from a get within the bot.on to decide if it exists or not, the error only calls up in the function (the line with set, any set in that function). I've tried making the function sync & async, passing playerDB in the call, using try/catch; Nothing changes, but it's only on the first time. If I restart the script after it's ran once with the rethink server up, it loads fine. I'm just not sure how to fix it?

It's hard to reproduce errors (still learning): I change something about the DB, I delete the rethink server data folder (remove clutter from the changed DB setup), run the rethink server, load the script and let it error, shut down the script, reload the script -- then no 'Insert' issue.

Also the reason I'm posting this issue here, I'm not sure how widely used Enmap-Rethink is, I assumed this would be the fastest way to get some help. I've attached app.js to this issue (in issue_file.zip), so the full scope is there. There is no sensitive code information.

@eslachance
Copy link
Owner

I... I don't understand at all what you're trying to do. Why would you possibly need to create an array of playerDB with one new enmap for each new player.. That makes no sense to me.

Did you maybe not realize that you could just... add objects to an enmap?
So instead of that weird code you have that pushes new enmaps into an array of enmaps, you could just... playerDB.set(M.user.id, {id: M.user.id, name: M.user.username, hexacoin: 10, exp: 0, level: 1});

I just feel your methodology is horribly backwards.

@eslachance
Copy link
Owner

Actually this is the sort of code that the more I look at, the more I need eye bleach because there is so much horrendous errors in there it's a wonder it even runs at all under any circumstances. Trash all this, start from scratch, this is garbage.

@varinyc
Copy link
Author

varinyc commented Mar 14, 2018

I was under the impression that this only works with key:pair lists, I couldn't save objects or JSON files to the database. Kept telling me values must be strings or numbers. So I just opted into making an array for the players, and worked nicely with indexing. It's all backwards because that's how I've been learning, very slowly. Thanks for the kind words.

@varinyc varinyc closed this as completed Mar 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants