PurplePanda
PurplePanda

Reputation: 683

How to get utility function from helper file on node.js server?

I have a node/express server and I'm trying to get a function from a helper file to my app.js for use. Here is the function in the helper file:

CC.CURRENT.unpack = function(value)
{
    var valuesArray = value.split("~");
    var valuesArrayLenght = valuesArray.length;
    var mask = valuesArray[valuesArrayLenght-1];
    var maskInt = parseInt(mask,16);
    var unpackedCurrent = {};
    var currentField = 0;
    for(var property in this.FIELDS)
    {
        if(this.FIELDS[property] === 0)
        {
            unpackedCurrent[property] = valuesArray[currentField];
            currentField++;
        }
        else if(maskInt&this.FIELDS[property])
        {
			//i know this is a hack, for cccagg, future code please don't hate me:(, i did this to avoid
			//subscribing to trades as well in order to show the last market
         	if(property === 'LASTMARKET'){
                unpackedCurrent[property] = valuesArray[currentField];
            }else{
                 unpackedCurrent[property] = parseFloat(valuesArray[currentField]);
            }
            currentField++;
        }
    }

    return unpackedCurrent;
};

At the bottom of that helper file I did a module.export (The helper file is 400 lines long and I don't want to export every function in it):

  module.exports = {
    unpackMessage: function(value) {
      CCC.CURRENT.unpack(value);
    }
  }

Then in my app.js I called

var helperUtil = require('./helpers/ccc-streamer-utilities.js');

and finally, I called that function in app.js and console.log it:

res = helperUtil.unpackMessage(message);
console.log(res);

The problem is that the console.log gives off an undefined every time, but in this example: https://github.com/cryptoqween/cryptoqween.github.io/tree/master/streamer/current (which is not node.js) it works perfectly. So I think I am importing wrong. All I want to do is use that utility function in my app.js

Upvotes: 0

Views: 1445

Answers (1)

Jay Edwards
Jay Edwards

Reputation: 1035

The unPackMessage(val) call doesn't return anything:

module.exports = {
  unpackMessage: function(value) {
    CCC.CURRENT.unpack(value);
  }
}

you need to return CCC.CURRENT.UNPACK(value);

module.exports = {
  unpackMessage: function(value) {
    return CCC.CURRENT.unpack(value);
  }
}

Upvotes: 1

Related Questions