NodeJS类型错误问题

时间:2018-02-25 11:06:12

标签: node.js typeerror

我一直在研究D& D角色创建器应用程序,以学习一些NodeJS。以下代码工作正常,直到我昨天在一个单独的文件中创建了一些新的无关函数(dice.js)。

现在,当从命令行运行node app.js newCharacter时,我得到以下TypeError:

enter image description here

大多数代码仍然只是骨架功能和WIP,但希望下面粘贴的代码提供足够的信息来指出我出错的地方。

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
}

当我从另一个文件(例如Dice.js)调用一个函数来设置Character.sex的值时程序运行成功,这很奇怪所以我最初认为我必须删除getRandSex函数来自module.exports中的utils.js,但正如您在下面所见,它仍在那里。

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
}

我对这种语言很陌生,所以我很可能忽略了一些非常基本的东西,任何帮助都会受到赞赏。

正如@Elmer Dantas所建议的那样,改变我导出函数的格式就可以了。

所以,我最初的方法:

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

更新和工作方法:

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

2 个答案:

答案 0 :(得分:1)

我认为您应该将导出更改为:

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

甚至

exports.getRandSex = getRandSex;
//and so on

答案 1 :(得分:0)

尝试 从'./util.js'导入{getRandSex}; 并在您的文件中使用它