1

I'm kind of new to JS and I can't solve this problem, so I hope you can help me. I will explain shortly what the situation is, I installed the app Homebridge from Github on my Raspberry: https://github.com/nfarina/homebridge

Installation was successful, so, so far so good. But then I installed the plugin eWeLink for the Homebridge app: https://github.com/gbro115/homebridge-ewelink the installation went good as well, but on the startup there seems to be a probleem in the index.js from the plugin, I get the following output:

[2018-5-31 23:10:37] [eWeLink] A total of [0] accessories were loaded from the local cache [2018-5-31 23:10:37] [eWeLink] Requesting a list of devices from eWeLink HTTPS API at [https://eu-ota.coolkit.cc:8080] [2018-5-31 23:10:37] Homebridge is running on port 51826. [2018-5-31 23:10:37] [eWeLink] eWeLink HTTPS API reports that there are a total of [108] devices registered /usr/lib/node_modules/homebridge-ewelink/index.js:98 body.forEach((device) => { ^

TypeError: body.forEach is not a function at /usr/lib/node_modules/homebridge-ewelink/index.js:98:22 at Object.parseBody (/usr/lib/node_modules/homebridge-ewelink/node_modules/request-json/main.js:74:12) at Request._callback (/usr/lib/node_modules/homebridge-ewelink/node_modules/request-json/main.js:148:26) at Request.self.callback (/usr/lib/node_modules/homebridge-ewelink/node_modules/request/request.js:186:22) at emitTwo (events.js:126:13) at Request.emit (events.js:214:7) at Request. (/usr/lib/node_modules/homebridge-ewelink/node_modules/request/request.js:1163:10) at emitOne (events.js:116:13) at Request.emit (events.js:211:7) at IncomingMessage. (/usr/lib/node_modules/homebridge-ewelink/node_modules/request/request.js:1085:12)

So the terminal tells me there is a error on line 98 from the index.js, that will be the next part of the script:

var devicesFromApi = new Map();
var newDevicesToAdd = new Map();

body.forEach((device) => {
    platform.apiKey = device.apikey;
    devicesFromApi.set(device.deviceid, device);
});

// Now we compare the cached devices against the web list
platform.log("Evaluating if devices need to be removed...");

function checkIfDeviceIsStillRegistered(value, deviceId, map) {

    var accessory = platform.accessories.get(deviceId);

    if (devicesFromApi.has(deviceId)) {
        platform.log('Device [%s] is regeistered with API. Nothing to do.', accessory.displayName);
    } else {
        platform.log('Device [%s], ID : [%s] was not present in the response from the API. It will be removed.', accessory.displayName, accessory.UUID);
        platform.removeAccessory(accessory);
    }
}

I found some similar problems with the fromEach function but I still can't seem to figure out what I should change in the script.

Hope you can help me :)

Nico Van Belle
  • 4,911
  • 4
  • 32
  • 49
stehof
  • 15
  • 7

1 Answers1

1

body is not an Array, therefore you cannot invoke .forEach on it, you can try converting it like

Array.from(body).forEach(function (device) { ... }

Take a look on this answer that might help : forEach is not a function error with JavaScript array

Yaniv
  • 44
  • 4
  • So if I'm right, I should change the script on line 98 from: body.forEach((device) => { platform.apiKey = device.apikey; devicesFromApi.set(device.deviceid, device); }); To: Array.from(body).forEach(function (device) { platform.apiKey = device.apikey; devicesFromApi.set(device.deviceid, device); }); – stehof Jun 01 '18 at 11:01