karbassi / netatmo

A node.js module to hook into the netatmo API.
MIT License
58 stars 45 forks source link

Question: Options and callback parameters #72

Closed jeanbie0 closed 2 years ago

jeanbie0 commented 2 years ago

Hello,

I've one question about a part of code in each api function:

if (options != null && callback == null) {
  callback = options;
  options = null;
}

What exactly does this? When we use code from example:

// Get Thermostats Data
// See docs: https://dev.netatmo.com/dev/resources/technical/reference/thermostat/getthermostatsdata
var options = {
  device_id: '',
};

api.getThermostatsData(options);

This part of code erase "options", so options are not longer take into account. And this part of code create a 'bad' callback with the option parameter. This give this error because callback from return callback(err, devices); is not a function, it's option.

/home/user/Scripts/netatmo/node_modules/netatmo/netatmo.js:276
      return callback(err, devices);
             ^

TypeError: callback is not a function
    at netatmo.<anonymous> (/home/user/Scripts/netatmo/node_modules/netatmo/netatmo.js:276:14)
    at Request.self.callback (/home/user/Scripts/netatmo/node_modules/request/request.js:185:22)
    at Request.emit (node:events:390:28)
    at Request.<anonymous> (/home/user/Scripts/netatmo/node_modules/request/request.js:1154:10)
    at Request.emit (node:events:390:28)
    at IncomingMessage.<anonymous> (/home/user/Scripts/netatmo/node_modules/request/request.js:1076:12)
    at Object.onceWrapper (node:events:509:28)
    at IncomingMessage.emit (node:events:402:35)
    at endReadableNT (node:internal/streams/readable:1343:12)
    at processTicksAndRejections (node:internal/process/task_queues:83:21)

Thank you.

karbassi commented 2 years ago

Hello @jeanbie0,

Good question.

There are two ways to use this library. One is with callbacks and the other is with a listener.

Try the following:

// Get Thermostats Data
// See docs: https://dev.netatmo.com/dev/resources/technical/reference/thermostat/getthermostatsdata
var options = {
  device_id: '',
};

api.getThermostatsData(options, function(err, devices){
  // You code here.
});
jeanbie0 commented 2 years ago

Yes I understood this. But if I want to use options and listeners (so no callback), I'll have

options != null
callback == null

So it'll enter in the condition and options will be set to null. So options are not taken into account.

The example is not working, if I write a device_id, it'll be erased.

karbassi commented 2 years ago

@jeanbie0 could you share your code snippet so I can help you?

jeanbie0 commented 2 years ago
var netatmo = require('netatmo');

const home_id = "abcdef0123456789";

var auth = {
  "client_id": credentials.client_id,
  "client_secret": credentials.client_secret,
  "username": credentials.username,
  "password": credentials.password,
};

var api = new netatmo(auth);

var getHomeStatus = function(err, data) {
    console.log("getHomeStatus");
    console.log(data.home.rooms[0]);
};

api.on('get-homestatus', getHomeStatus);

var options = {
  home_id: home_id,
};
api.getHomeStatus(options);

This return me Netatmo threw a warning: Error: getHomeStatus error: Missing home_id. If I comment at line 696 from netatmo.js, everything works well.

Thank you