Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'apply' of undefined #1

Open
DoomHawk opened this issue Aug 24, 2017 · 1 comment
Open

TypeError: Cannot read property 'apply' of undefined #1

DoomHawk opened this issue Aug 24, 2017 · 1 comment

Comments

@DoomHawk
Copy link

Stack trace from error:

at Object emit (.../stampit-event-bus/src/stampit-event-bus.js:22:37)
at Object.<anonymous> (.../jsmodbus/src/modbus-tcp-client.js:55:12)
at emitNone (events.js:91:20)
at Socket.emit (events.js:185:7)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1073:10)

This happens when I start my server which utilizes the jsmodbus package. I've run into this issue on two deployments now; the first one, I'm ashamed to admit, I edited your source code to just check if (cbList[name][i]) before the erroring line 24. I ran into the issue again today when deploying a new server and thought I'd send in an issue. I did some debugging of my own and found that the issue happens in this section of the stampit-event-bus.js:

for (var i in cbList[name]) {
    cbList[name][i].listener.apply(this, args);
    // remove if once === true
    if (cbList[name][i] && cbList[name][i].once) {
        cbList[name].splice(i, 1);-+}
    }
}

If I add console.log(i) before that cbList[name][i].listener.apply(this, args); line, I can see that this issue doesn't happen when i represents integer values (usually 0, even though technically it is typeof string at that moment) but rather that the error fires when the value of i is "getIndexBy". I tried, but it is beyond my depth to determine for certain where that 'getIndexBy" is coming from or why. For the moment, to get my system running, I have added (based on MDN articles about for...in usage) a hasOwnProperty check to make that for loop:

for (var i in cbList[name]) {
    if (cbList[name].hasOwnProperty(i)) {
        cbList[name][i].listener.apply(this, args);
    };
    // remove if once === true
    if (cbList[name][i] && cbList[name][i].once) {
        cbList[name].splice(i, 1);
    }
}

With my patched line making sure that the key in i for that loop iteration actually exists as a property of cbList[name], everything seems to work correctly; the listener.apply line still executes as it would have before; but the error is eliminated.

FWIW: this is running on debian jesse on a Raspberry Pi 3 (and getting jsmodbus, more specifically its serialport dependency, to install on Raspberry Pi is a TRICK and a half since the new PIXEL RPi OS came out)

Thanks for your assistance and all your great work, the jsmodbus library is extremely useful to me!

@stefanpoeter
Copy link
Member

Can you give me a code example so that I can reproduce the error? It seems like this error is coming from the jsmodbus lib. Can you reproduce this without jsmodbus?

What node version are you using?

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

No branches or pull requests

2 participants