Get the current number of data registers for the device

Hi there,

I am using account.bucket.amount followed in this documentation: Buckets | TagoIO SDK for JavaScript and TypeScript
I have this following line in my code:

const { Analysis, Device, Services, Utils, Account } = require("@tago-io/sdk");
const axios = require(“axios”);
const dayjs = require(“dayjs”);

async function myAnalysis(context) {

    const account = new Account({ token: "..." });
    const device_id = "5fa8a25464cead00287c1626";
    const device_bucket = await account.bucket.amount(device_id);
    context.log("device bucket: " + device_bucket)

}

I am not sure if this is the correct way to grab the data but I am only getting {} as an output. If you can go into more detail regarding this. I would greatly appreciate it.

Thanks!
Daniel

Hi Daniel,
I erased your account token from the post, as other people can get it and use to get access to your account.

You can receive {} in the analysis console if you did something very wrong in the code.
One way to debug it is to add a try catch function to your analysis, as:

const { Analysis, Device, Services, Utils, Account } = require("@tago-io/sdk");
const axios = require("axios");
const dayjs = require("dayjs");

async function myAnalysis(context) {
  try {

    const account = new Account({ token: "..." });
    const device_id = "603699bc9cc2f300113a963c";
    const device_bucket = await account.bucket.amount(device_id);
    context.log("device bucket: " + device_bucket)
  } catch (e) {
    context.log(e.message || JSON.stringify(e));
  } 
}

module.exports = new Analysis(myAnalysis);

That will output the following error to the console:
[2021-02-24 16:35:25] Cannot read property ‘amount’ of undefined

Which is happening because you are using account.bucket.amount instead of account.buckets.amount

1 Like

I also recommend you to try running analysis in external, as it makes much more easier to debug this kind of error:

Running Analysis as External using Node.JS - TagoIO

1 Like

Ok, that fixes the {} error. I am now getting “Bucket can’t be found” in console. But, it’s probably something I can figure out. Thanks!

You must provide the bucket ID, and not the device ID.

Ok that fixed it. Thanks!