Search code examples
javascriptnode.jshubot

Trouble assigning multiple nodejs exports return values to variables


I have two files, a main file and a module file. Inside the module file, I export two functions, which return objects. In the main file, I am setting a variable to the return value of each method. However, each variable is set to the return value of the last method called. What am I doing wrong here?

/* main file */
module.exports = function(robot) {
  var startingCharacter = require('./data/starting/character.json'),
      make = require('./includes/make.js');

  return robot.respond(/dungeon me/i, function(msg) {
    var character = startingCharacter,
        weapon = make.weapon(0),
        armor = make.armor(0);

    console.log(weapon); // <- returns value from make.armor(0)
    console.log(armor); // <- returns value from make.armor(0)
  }
};


/* module file */
var random = require('./random.js');

var baseStats = {
  'equipped': 'true',
  'attack': '0',
  'damage': '0',
  'defense': '0',
  'health': '0',
  'luck': '0',
  'passivePower': {},
  'activePower': {}
};

// Generate a weapon
exports.weapon = function(level) {
  var weaponNames = require('../data/nouns/weapon_names.json');
  var thisWeapon = {};

  if (level === 0) {
    var adjNeg = require('../data/adjectives/negative.json');

    thisWeapon = baseStats;
    thisWeapon.name = adjNeg[random(0, adjNeg.length)] + ' ' +  weaponNames[random(0, weaponNames.length)];
    thisWeapon.type = 'weapon';
  }

  console.log(thisWeapon); // <- returns correct value
  return thisWeapon;
};

// Generate armor
exports.armor = function(level) {
  var armorNames = require('../data/nouns/armor_names.json');
  var thisArmor = {};

  if (level === 0) {
    var adjNeg = require('../data/adjectives/negative.json'),
        animals = require('../data/nouns/animals.json');

    var armorName = armorNames[random(0, armorNames.length)];

    thisArmor = baseStats;

    if (armorName === 'Hide') {
      thisArmor.name = animals[random(0, animals.length)] + ' ' + armorName + ' armor';
    }
    else {
      thisArmor.name = adjNeg[random(0, adjNeg.length)] + ' ' + armorName;
    }
    thisArmor.type = 'armor';
  }

  console.log(thisArmor); // <- returns correct value
  return thisArmor;
};

Note: edited with real code, instead of sample code


Solution

  • When you do this:

    thisArmor = baseStats;
    

    you're not making a copy of the "baseStats" object. Since both methods in the module modify and return a reference to that same object, you get the effect you observe.

    There are several ways to implement a shallow copy operation for Node; see this Stackoverflow question. With one of those approaches, you'd start those methods off with a local copy of "baseStats".