javascript - Node.js getting around 'listener must be a function' error -
this question has answer here:
i using node.js , steam-node write couple of bots steam (stored on array), each bot has own account , stuff. so, begin, here's part of code:
function onlogon(index){ console.log('[steam] logged in on bot ' + index); bots[index].setpersonastate(steam.epersonastate.online); /*do other stuff*/ } (var = 0; < bots.length; i++){ /*foreach of bots, assign loggedon listener */ bots[i].on('loggedon', onlogon(i)); } and code giving me 'listener must function'. now, know error means, should set event listener this:
bots[i].on('loggedon', onlogon); but doesnt work, because need pass variable event.
i this:
for (var = 0; < accounts.length; i++){ bots[i].on('loggedon', function() { console.log('[steam] logged in on bot ' + i); bots[i].setpersonastate(steam.epersonastate.online); //... }); } but doesnt work because passed reference , throws typeerror: cannot read property 'setpersonastate' of undefined way.
bots[i].on('loggedon', (function(index) { console.log('[steam] logged in on bot ' + i); bots[index].setpersonastate(steam.epersonastate.online); //... })(i)); and doesn't work...
is there way want here? or should dont use arrays?
when run
bots[i].on('loggedon', onlogon(i)); you immediately calling onlogon, , passing result on. want bind first argument without calling it, can done follows:
bots[i].on('loggedon', onlogon.bind(null, i)); the null because first argument bind context (or this value), don't care about.
Comments
Post a Comment