mangoose002 / Meross2Domoticz

NodeJS bridge between Meross IOT and Domoticz
GNU Lesser General Public License v3.0
5 stars 2 forks source link

Crash if No dummy hardware created in Domoticz #8

Closed jeremielec closed 4 years ago

jeremielec commented 4 years ago

In the case of the script is started without any dummy hardware in Domoticz, this ons crash with

TypeError: Cannot read property 'filter' of undefined
    at Request._callback (/mnt/mmcblk0p3/plugins/Meross2Domoticz/meross2domoticz.js:73:40)
    at Request.self.callback (/mnt/mmcblk0p3/plugins/Meross2Domoticz/node_modules/request/request.js:185:22)
    at emitTwo (events.js:126:13)
    at Request.emit (events.js:214:7)
    at Request.<anonymous> (/mnt/mmcblk0p3/plugins/Meross2Domoticz/node_modules/request/request.js:1154:10)
    at emitOne (events.js:116:13)
    at Request.emit (events.js:211:7)
    at IncomingMessage.<anonymous> (/mnt/mmcblk0p3/plugins/Meross2Domoticz/node_modules/request/request.js:1076:12)
    at Object.onceWrapper (events.js:313:30)
    at emitNone (events.js:111:20)

At line 73 there is a control doing too late :

    var hardware = domohardware.result.filter( ob => { return ob.Type == 15; });

    if(hardware && Array.isArray(hardware) && hardware.length > 0){
        hardware = hardware.pop();
        DummyHardwareId = hardware.idx;
        LogToConsole(debug,"Dummy hardware found (" + DummyHardwareId + "). Autocreate enabled");

    } else {
        autocreate = false;
        LogToConsole(debug,"No dummy hardware found. Autocreate disabled");
    }

Fixed code :


  if (domohardware.result == undefined)
    {
         autocreate = false;
        LogToConsole(debug,"No dummy hardware found. Autocreate disabled");
        return;
    }

    var hardware = domohardware.result.filter( ob => { return ob.Type == 15; });

    if(hardware && Array.isArray(hardware) && hardware.length > 0){
        hardware = hardware.pop();
        DummyHardwareId = hardware.idx;
        LogToConsole(debug,"Dummy hardware found (" + DummyHardwareId + "). Autocreate enabled");

    } else {
        autocreate = false;
        LogToConsole(debug,"No dummy hardware found. Autocreate disabled");
    }
mangoose002 commented 4 years ago

You are completely right, I added your code and will push it soon.

mangoose002 commented 4 years ago

The code was pushed today. It should prevent from this error.