Reputation: 215
Hi can someone tell me why I can't instantiate the following class/object?
function Arbitrage(odds1, odds2, investment)
{
this.investment = investment;
this.odds1 = odds1;
this.odds2 = odds2;
this.arbPercent = function() {
return 1.0/this.odds1 + 1.0/this.odds2;
};
this.profit = function() {
return this.investment / this.arbPercent() - this.investment;
};
this.individualBets = function() {
return {
odds1bet : this.investment/this.odds1/this.arbPercent(),
odds2bet : this.investment/this.odds2/this.arbPercent()
};
};
};
module.exports = Arbitrage;
I'm calling it like this:
var utility = require('../businesslogic/utility');
...
router.post('/calculate', function(req, res)
{
var arbit = new utility.Arbitrage(req.body.odds1, req.body.odds2, req.body.investment);
res.json({
arbPercentage : arbit.arbPercent(),
profit : arbit.Profit(),
indvBets : arbit.individualBets()
});
});
The first line, var arbit = new utility.Arbitrage(...)
is throwing the error.
It says TypeError: undefined is not a function
I've checked that utility
isn't null or anything like that. Also all the constructor arguments are ok.
I'm not very familiar with javascript, any help would be appreciated. Thanks.
Upvotes: 1
Views: 370
Reputation: 150
it's because of the way you exported it
you should use :
module.exports.Arbitrage = Arbitrage;
And then you can instanciate like this :
var Arbitrage = require('../businesslogic/utility');
var varbitrage = new Arbitrage();
Upvotes: -1
Reputation: 51460
You're exporting your Arbitrage
class directly, so after you're requiring it
var utility = require('../businesslogic/utility');
utility
is actually your Arbitrage
class, meaning that typeof utility === 'function'
.
I can see two ways of fixing it.
1. Change the way you're requiring your Arbitrage
class:
var Arbitrage = require('../businesslogic/utility');
// ...
var arbit = new Arbitrage(...);
2. Or change the way you're exporting it:
exports.Arbitrage = Arbitrage;
Upvotes: 3