melanke / Watch.JS

watch the changes of any object or attribute
Other
2.14k stars 219 forks source link

pushing to array crashes watchjs #118

Closed utillity closed 6 years ago

utillity commented 7 years ago

this is my code adding a new entry to state.connections:

function Connection(ip, desc) {
    this.ipAddress = ip;
    this.description = desc;
    this.status = "Active";
    this.connectedSince = Date.now();
}

var getOrCreateConnection = function(request) {
    var ip = getClientAddress(request);
    var conn = state.connections.find(e => e.ipAddress == ip);
    if (conn != null)
    {
        if (conn.status != "Active")
        {
            conn.status = "Active";
            conn.connectedSince = Date.now();
            console.log("Connection reestablished with " + ip);
        }
    }
    else
    {
        console.log("Connection established with " + ip);
        conn = new Connection(ip, "Unknown");
        state.connections.push(conn);
    }
    return conn;
}

on pushing conn to state.connections, I get the following error:

C:\DEV\uTILLIty\DP\MK4\GOP.UI\Server\node_modules\melanke-watchjs\src\watch.js:114
            copy[attr] = obj[attr];
                       ^

TypeError: Cannot set property 'ipAddress' of undefined
    at clone (C:\DEV\uTILLIty\DP\MK4\GOP.UI\Server\node_modules\melanke-watchjs\src\watch.js:114:24)
    at pushToLengthSubjects (C:\DEV\uTILLIty\DP\MK4\GOP.UI\Server\node_modules\melanke-watchjs\src\watch.js:735:22)
    at watchOne (C:\DEV\uTILLIty\DP\MK4\GOP.UI\Server\node_modules\melanke-watchjs\src\watch.js:255:13)
    at watchMany (C:\DEV\uTILLIty\DP\MK4\GOP.UI\Server\node_modules\melanke-watchjs\src\watch.js:235:13)
    at Timeout.loop [as _onTimeout] (C:\DEV\uTILLIty\DP\MK4\GOP.UI\Server\node_modules\melanke-watchjs\src\watch.js:681:29)
    at ontimeout (timers.js:386:14)
    at tryOnTimeout (timers.js:250:5)
    at Timer.listOnTimeout (timers.js:214:5)
englishextra commented 7 years ago

that's because if (conn != null) - check staright for null or undefined. It's up to you.

utillity commented 7 years ago

sorry, I don't get it. Changing code to the following yields same result (error and crash):

    if (conn == null)
    {
        console.log("Connection established with " + ip);
        conn = new Connection(ip, "Unknown");
        state.connections.push(conn);
    }
    else
    {
        if (conn.status != "Active")
        {
            conn.status = "Active";
            conn.connectedSince = Date.now();
            console.log("Connection reestablished with " + ip);
        }
    }
englishextra commented 7 years ago

Cannot comment if I haven't a demo or test repo.

utillity commented 7 years ago

ok, found the problem. my class has a ctor with parameters. Had to add another ctor which would also init the properties. Not very nice to have to do that, though.

function Connection(ip, desc) {
    this.ipAddress = ip;
    this.description = desc;
    this.status = "Active";
    this.connectedSince = Date.now();
}
Connection.prototype = {
    ipAddress: null,
    description: null,
    status: "Active",
    connectedSince: Date.now()
};
englishextra commented 7 years ago
undefined != null  // false
undefined !== null // true
englishextra commented 7 years ago

the mistake is here if (conn == null)

melanke commented 6 years ago

If I understand it right the problem was solved, I am closing this issue. Reopen if needed.