zenbot icon indicating copy to clipboard operation
zenbot copied to clipboard

Error: Cannot read property 'toString' of undefined

Open lomop opened this issue 9 years ago • 13 comments

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)

lomop avatar Aug 08 '17 12:08 lomop

What was the command you were trying to run?

DeviaVir avatar Aug 08 '17 12:08 DeviaVir

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

lomop avatar Aug 08 '17 12:08 lomop

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)

asterismo avatar Aug 10 '17 00:08 asterismo

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 avatar Aug 10 '17 00:08 shadowrider26

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

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

maybe???

shadowrider26 avatar Aug 12 '17 06:08 shadowrider26

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

codabrink avatar Aug 17 '17 04:08 codabrink

Can we fix this with a pull request

brucetus avatar Oct 17 '17 17:10 brucetus

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

kayoshin avatar Dec 10 '17 15:12 kayoshin

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...

haxwell avatar Dec 18 '17 23:12 haxwell

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.

KryptoNova avatar Dec 19 '17 00:12 KryptoNova

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

haxwell avatar Dec 19 '17 01:12 haxwell

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.

KryptoNova avatar Dec 19 '17 02:12 KryptoNova

@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: https://github.com/carlos8f/zenbot/pull/930

DeviaVir avatar Dec 19 '17 09:12 DeviaVir