jens-maus / node-unifi

NodeJS class for querying/controlling a UniFi-Controller (UDM-Pro, UDM-SE, UDM, UDR, UDW, CloudKey Gen1/Gen2) from Ubiquiti (www.ui.com)
MIT License
144 stars 50 forks source link

Return voucher create_time #48

Closed SpaceDux closed 3 years ago

SpaceDux commented 4 years ago

Hi, I've just started looking into this, I previously used the PHP version, however I can't find any documentation of your JS build.

Generating the voucher is working as expected, which is great, however the callback/return isn't being piped? Am I missing something, or doing something incorrect?

var unifi = require('node-unifi');
var controller = new unifi.Controller(config.UBQT.Address, config.UBQT.Port);

let thisclass = {
  GenerateVoucher: function()
  {
    controller.login(config.UBQT.User, config.UBQT.Password, function(err) {
      if(err) {
        console.log('ERROR: ' + err);
        return;
      }

      controller.createVouchers(config.UBQT.Site, config.UBQT.Expiry, '', "1", "1", "WiFi Voucher via Node", config.UBQT.MaxUp, config.UBQT.MaxDown, function(err, result) {
        if(err)
        {
          console.log(err);
          return;
        }
        alert('data: '+ result);
        controller.logout();
      });
    });
  }
}

module.exports = thisclass;

within this function, what is "cb"?

Any help on this would be greatly appreciated.

Thank you 👍

jens-maus commented 4 years ago

See here for an example use case:

https://github.com/iobroker-community-adapters/ioBroker.unifi/blob/master/main.js#L1123

Apart from that cb is of course the callback function you need to specify. Thus, you should use the following function call:

controller.createVouchers(config.UBQT.Site, config.UBQT.Expiry, function(err, result) {
   ...
}, '', "1", "1", "WiFi Voucher via Node", config.UBQT.MaxUp, config.UBQT.MaxDown);

Thus, note the different position of the function(err, result) callback function...

SpaceDux commented 4 years ago

Hi Jens,

Great & very efficient response! I kinda guessed CB would mean callback. But ofcourse I was doing it incorrectly. Your response was all I needed to get on track. Good job on the package, it seems great 👍 .

My code now looks like this now;

var unifi = require('node-unifi');
var controller = new unifi.Controller(config.UBQT.Address, config.UBQT.Port);

let thisclass = {
  GenerateVoucher: function()
  {
    return new Promise(function(resolve, reject) {
      controller.login(config.UBQT.User, config.UBQT.Password, function(err) {
        if(err) {
          console.log('ERROR: ' + err);
          reject(err);
        }
        controller.createVouchers(config.UBQT.Site, config.UBQT.Expiry, async function(err, result) {
          if(err)
          {
            console.log(err);
            reject(err);
          }
          let response = await result[0];
          controller.getVouchers(config.UBQT.Site, async function(err, result) {
            if(err)
            {
              console.log(err);
              reject(err);
            }

            let return_codes = await result[0];
            console.log("Code: " + return_codes[0].code);
            resolve(return_codes[0].code);
            controller.logout();
          }, response[0].create_time)
        }, "1", "1", "Generated via NodeJS", config.UBQT.MaxUp, config.UBQT.MaxDown);
      });
    });
  }
}

module.exports = thisclass;

Hopefully the above may help another person that hit the same wall as I did :).

Thanks again Jens, appreciate the support.