hobbyquaker / lgtv2mqtt

Interface between LG WebOS Smart TVs and MQTT :tv:
MIT License
106 stars 34 forks source link

TypeError: Cannot read properties of undefined (reading 'indexOf') #18

Open michelscholte opened 1 year ago

michelscholte commented 1 year ago

I'm getting this error. Is there a solution for it? Thank you

/usr/lib/node_modules/lgtv2mqtt/index.js:137 if (res.changed.indexOf('volume') !== -1) { ^

TypeError: Cannot read properties of undefined (reading 'indexOf') at Object.bed14b150001 (/usr/lib/node_modules/lgtv2mqtt/index.js:137:25) at WebSocketConnection. (/usr/lib/node_modules/lgtv2mqtt/node_modules/lgtv2/index.js:149:48) at WebSocketConnection.emit (node:events:513:28) at WebSocketConnection.processFrame (/usr/lib/node_modules/lgtv2mqtt/node_modules/websocket/lib/WebSocketConnection.js:554:26) at /usr/lib/node_modules/lgtv2mqtt/node_modules/websocket/lib/WebSocketConnection.js:323:40 at processTicksAndRejections (node:internal/process/task_queues:78:11)

Rybun commented 1 year ago

Hi, I changed the following from the 137 line of file /usr/lib/node_modules/lgtv2mqtt/index.js :

From:

        if (res.changed.indexOf('volume') !== -1) {
            mqtt.publish(config.name + '/status/volume', String(res.volume), {retain: true});
        }
        if (res.changed.indexOf('muted') !== -1) {
            mqtt.publish(config.name + '/status/mute', res.muted ? '1' : '0', {retain: true});
        }
    });

To:

        if (res && res.changed && res.changed.indexOf('volume') !== -1) {
            mqtt.publish(config.name + '/status/volume', String(res.volume), {retain: true});
        }
        if (res && res.changed && res.changed.indexOf('muted') !== -1) {
            mqtt.publish(config.name + '/status/mute', res.muted ? '1' : '0', {retain: true});
        }
    });

After that, It seems to work.