Christopher Long
Christopher Long

Reputation: 904

NodeJS Type Error issue

I have been working on a D&D Character creator app to learn a bit of NodeJS. The following code was working fine until I created a few new unrelated functions in a separate file yesterday (dice.js).

Now when running node app.js newCharacter from the command line I get the following TypeError:

enter image description here

Most of the code is still just skeleton functions and WIP, but hopefully the pasted code below provides enough information to point out where I'm going wrong.

app.js

const fs = require('fs');
const _ = require ('lodash');
const yargs = require('yargs');

const utils =require('./utils.js');
const dice =require('./dice.js');
const character =require('./Character.js');
const races =require('./races.js');

const argv = yargs.argv;
var command = argv._[0];

if (command === 'newCharacter') {
  console.log('----------------------');
  console.log('Creating new character');
  console.log('----------------------');
  var randomCharacter = character.newCharacter();
  console.log(randomCharacter);
  console.log('----------------------');

} else if (command === 'listCharaters'){
  console.log('listing exsiting charaters');
}else if (command === 'removeCharater'){
  console.log('Removing charater');
}else if (command === 'rollDice'){
  console.log("Roll: ", character.rollDice());
}else if (command === 'testFunc'){
  console.log(utils.getRandSex());
}else{
  console.log('Unrecognised command');
}

Character.js

const dice =require('./dice.js');
const utils =require('./utils.js');
const app =require('./app.js');
const races =require('./races.js');

const _ = require ('lodash');
const yargs = require('yargs');

const argv = yargs.argv;

var newCharacter = () => {
var Character = {};

  Character["Sex"] = utils.getRandSex();

return Character;
};

module.exports = {
newCharacter
}

strangely enough when I call a function from another file (such as Dice.js) to set the value of Character.sex the program runs successfully, so I initially thought I must have removed the getRandSex function from the module.exports in utils.js, but as you can see below it's still in there.

utils.js

const races =require('./races.js');
const dice =require('./dice.js');
const classes =require('./classes.js');
const character =require('./Character.js');
const _ = require ('lodash');

var listChar = () => {}
var removeChar = () => {}
var getChar = () => {}
var calcBaseHP = () => {}
var calcMovementSpeed = () => {}

var getRandName = (race,sex) => {

  if (race === "Human" && sex === "Male"){
    var firstName = _.sample(races.humanMaleNames);
    var lastName  = _.sample(races.humanLastNames) ;
    return (firstName + " " + lastName);
  }
  else if (race === "Human" && sex === "Female"){
    var firstName = _.sample(races.humanFemaleNames);
    var lastName  = _.sample(races.humanLastNames) ;
    return (firstName + " " + lastName);
  }
  else if (race === "Dwarf" && sex === "Male"){
    var firstName = _.sample(races.dwarfMaleNames);
    var lastName  = _.sample(races.dwarfLastNames);
    return (firstName + " " + lastName);
  }
  else if (race === "Dwarf" && sex === "Female"){
    var firstName = _.sample(races.dwarfFemaleNames);
    var lastName  = _.sample(races.dwarfLastNames);
    return (firstName + " " + lastName);
  }
  else return "race not found" +" "+ race;
}

var getRandRace = () => {
  return _.sample(races.race);
}

var getRandClass = () => {
  return _.sample(classes.classes);
}

var getRandAlignment = () => {
  return _.sample(races.alignment);
}

var getRandSex = () => {
  return _.sample(races.sex);
}


var calcBonus = (stat) => {
  var statMod;
  if(stat === 1){
    statMod = -5;
    return statMod;
  }
  else if(stat >= 2 && stat <= 3){
    statMod = -4;
    return statMod;
  }
  else if(stat >= 4 && stat <= 5){
    statMod = -3;
    return statMod;
  }
  else if(stat >= 6 && stat <= 7){
    statMod = -2;
    return statMod;
  }
  else if(stat >= 8 && stat <= 9){
    statMod = -1;
    return statMod;
  }
  else if (stat >= 10 && stat <=11){
    statMod = 0;
    return statMod;
  }
  else if (stat >= 12 && stat <=13){
    statMod = +1;
    return statMod;
  }
  else if (stat >= 14 && stat <=15){
    statMod = +2;
    return statMod;
  }
  else if (stat >= 16 && stat <=17){
    statMod = +3
    return statMod;
  }
  else if (stat >= 18 && stat <=19){
    statMod = +4
    return statMod;
  }
  else if (stat >= 20 && stat <=21){
    statMod = +5;
    return statMod;
  }
  else if (stat >= 22 && stat <=23){
    statMod = +6;
    return statMod;
  }
  else if (stat >= 24 && stat <=25){
    statMod = +7;
    return statMod;
  }
  else if (stat >= 26 && stat <=27){
    statMod = +8;
    return statMod;
  }
  else if (stat >= 28 && stat <=29){
    statMod = +9;
    return statMod;
  }
  else if (stat === 30){
    statMod = +10;
    return statMod;
  }
  else{
    return "not caught";
  }
}


var assignStats = (x) => {
  var character = {};
  charater = x;
  if(character.class === "Barbarian")
  {
  var stats = dice.statList();
  stats.sort(function(a,b){return b - a});

  return character.str = stats[0];
  return character.con = stats[1];
  return character.dex = stats[2];
  return character.wis = stats[3];
  return character.cha = stats[4];
  return character.int = stats[5];
  }


}

module.exports = {
getRandSex,
calcBonus,
getRandSex,
getRandAlignment,
getRandClass,
assignStats,
getRandRace,
getRandName
}

I'm very new to this language so it's probable I'm overlooking something very basic, any assistance would be appreciated.

SOLUTION:

As advised by @Elmer Dantas, changing the format in which I exported my functions did the trick.

So, my original approach:

module.exports = {
getRandSex,
calcBonus,
getRandSex,
getRandAlignment,
getRandClass,
assignStats,
getRandRace,
getRandName
}

And the update and working approach:

exports.getRandSex = getRandSex;
exports.calcBonus = calcBonus;
exports.getRandAlignment = getRandAlignment;
exports.getRandClass = getRandClass;
exports.assignStats = assignStats;
exports.getRandClass = getRandClass;
exports.getRandRace = getRandRace;
exports.getRandName = getRandName;

Upvotes: 0

Views: 250

Answers (2)

Elmer Dantas
Elmer Dantas

Reputation: 4869

I think you should change your export to be like this:

module.exports = {
    getRandSex: function(){},
    method2: function(){}
}

or even

exports.getRandSex = getRandSex;
//and so on

Upvotes: 1

ravi
ravi

Reputation: 1145

Try import {getRandSex} from './util.js'; and use it in your file

Upvotes: 0

Related Questions