Skip to content
This repository has been archived by the owner on Feb 15, 2022. It is now read-only.

Error: Cannot read property 'toString' of undefined #457

Closed
lomop opened this issue Aug 8, 2017 · 13 comments
Closed

Error: Cannot read property 'toString' of undefined #457

lomop opened this issue Aug 8, 2017 · 13 comments
Labels

Comments

@lomop
Copy link

lomop commented Aug 8, 2017

v4.0.5

undefined
/home/anon/zenbot/node_modules/numbro/numbro.js:1111
        var parts = x.toString().split('.');
                     ^

TypeError: Cannot read property 'toString' of undefined
    at multiplier (/home/anon/zenbot/node_modules/numbro/numbro.js:1111:22)
    at /home/anon/zenbot/node_modules/numbro/numbro.js:1127:22
    at Array.reduce (native)
    at correctionFactor (/home/anon/zenbot/node_modules/numbro/numbro.js:1125:21)
    at Object.add (/home/anon/zenbot/node_modules/numbro/numbro.js:1197:47)
    at /home/anon/zenbot/commands/trade.js:234:78
    at /home/anon/zenbot/lib/engine.js:191:14
    at /home/anon/zenbot/extensions/exchanges/bittrex/exchange.js:113:18
    at Request._callback (/home/anon/zenbot/node_modules/node.bittrex.api/node.bittrex.api.js:112:25)
    at Request.self.callback (/home/anon/zenbot/node_modules/request/request.js:188:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (/home/anon/zenbot/node_modules/request/request.js:1171:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (/home/anon/zenbot/node_modules/request/request.js:1091:12)
    at IncomingMessage.g (events.js:292:16)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:185:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:80:11)
    at process._tickCallback (internal/process/next_tick.js:104:9)
@DeviaVir
Copy link
Owner

DeviaVir commented Aug 8, 2017

What was the command you were trying to run?

@lomop
Copy link
Author

lomop commented Aug 8, 2017

Zenbot trade bittrex.USDT-BCC --period=5m

@asterismo
Copy link

I have the same problem, it occurs randomly on some bots once every several hours.

undefined /home/admin/zenbot_xrp/node_modules/numbro/numbro.js:1111
var parts = x.toString().split('.');
^

TypeError: Cannot read property 'toString' of undefined
at multiplier (/home/admin/zenbot_xrp/node_modules/numbro/numbro.js:1111:22)
at /home/admin/zenbot_xrp/node_modules/numbro/numbro.js:1127:22
at Array.reduce (native)
at correctionFactor (/home/admin/zenbot_xrp/node_modules/numbro/numbro.js:1125:21)
at Numbro.add (/home/admin/zenbot_xrp/node_modules/numbro/numbro.js:1197:47)
at /home/admin/zenbot_xrp/commands/trade.js:234:78
at /home/admin/zenbot_xrp/lib/engine.js:191:14
at /home/admin/zenbot_xrp/extensions/exchanges/bittrex/exchange.js:113:18
at Request._callback (/home/admin/zenbot_xrp/node_modules/node.bittrex.api/node.bittrex.api.js:112:25)
at Request.self.callback (/home/admin/zenbot_xrp/node_modules/request/request.js:188:22)
at emitTwo (events.js:125:13)
at Request.emit (events.js:213:7)
at Request. (/home/admin/zenbot_xrp/node_modules/request/request.js:1171:10)
at emitOne (events.js:115:13)
at Request.emit (events.js:210:7)
at IncomingMessage. (/home/admin/zenbot_xrp/node_modules/request/request.js:1091:12)
at Object.onceWrapper (events.js:314:30)
at emitNone (events.js:110:20)
at IncomingMessage.emit (events.js:207:7)
at endReadableNT (_stream_readable.js:1045:12)
at _combinedTickCallback (internal/process/next_tick.js:138:11)
at process._tickCallback (internal/process/next_tick.js:180:9)

@shadowrider26
Copy link

I made this go away by adding this line above node_modules/numbro/numbro.js:1111

if (!x) return 1;
var parts = x.toString().split('.');

@shadowrider26
Copy link

Seems that testing for falsy wasn't enough so ...

if (typeof x === 'undefined' || x === null || !x) {
return 1; }

maybe???

@codabrink
Copy link

codabrink commented Aug 17, 2017

Undefined and null are both already falsey in JS.
Also having this issue, adding the if (!x) return 1 fixed it for now.
Thanks!

@brucetus
Copy link
Contributor

Can we fix this with a pull request

@kayoshin
Copy link

Same here, the error occurs randomly on Bittrex, if (!x) return 1; solved it for now.

@haxwell
Copy link
Contributor

haxwell commented Dec 18, 2017

This error is happening because the value for 's.balance.asset' is undefined, on (presently) lines 140 and 244 of trade.js.

For posterity and clarity's sake, here is that line:
var tmp_balance = n(s.balance.currency).add(n(s.period.close).multiply(s.balance.asset)).format('0.00000000')

I verified this with a small test program in node...

var n = require('numbro')

function main() {
	var currency = process.argv[2] === 'undefined' ? undefined : process.argv[2]
	var periodClose = process.argv[3] === 'undefined' ? undefined : process.argv[3]
	var asset = process.argv[4] === 'undefined' ? undefined : process.argv[4]

	var v = n(currency).add(n(periodClose).multiply(asset)).format('0.00000000')

	console.log(v)
}

main()

If you run that program, any value you pass in will be successful, except for an 'undefined' for the 'asset' variable. In that failing case, the resulting exception matches our situation here.

This variable is likely being set incorrectly somewhere in the Bittrex exchange object, when engine.js makes a call to Bittrex exhange's getBalance() method, from the engine.js :: syncBalance() method, currently on line 184. But I'm not 100% on that.

Regardless it seems like some bad data may be coming from the API. To fix it, we should wrap the call that sets tmp_balance, in trade.js, in a method and ensure that s.balance.asset is not undefined, otherwise set it to 0, before making the call. That will prevent the exception.

Once I get home this evening, I will make a PR for this...

@KryptoNova
Copy link
Contributor

I already have a PR in for the numbro repository owned by carlos8f. See: Fix Issue #457 for Zenbot Repository

If that is merged, then the if (!x) return 1 fix will be pulled the next time npm update is run for zenbot.

The package.json is pointing to that version of numbro. This seems to be a very reliable fix.

Is there anyone that can approve that pull request?
If not, I could put in a pull request to point to the fix in my forked version of carlos8f's numbro that is waiting for the pull request to be merged.

@haxwell
Copy link
Contributor

haxwell commented Dec 19, 2017

@KryptoNova Agreed. I think that is a clean fix.

@KryptoNova
Copy link
Contributor

I'm not sure, but if @DeviaVir could approve the pull request mentioned at Fix Issue #457 for Zenbot Repository then we should be in business.

@DeviaVir, If this cannot be done, let me know. I do have a Plan B if you do not have the ability to approve those pull requests.

@DeviaVir
Copy link
Owner

DeviaVir commented Dec 19, 2017

@KryptoNova unfortunately I don't have the permissions. Want me to fork it and we'll use that fork instead?

edit: forked to https://github.com/highvelocityspace/numbro
I'll PR us using that source.
edit: #930

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

9 participants