Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 TypeError: Cannot read properties of undefined (reading '1') #33

Closed
harrisonhjones opened this issue Feb 8, 2022 · 1 comment

Comments

@harrisonhjones
Copy link

So I ran the example in the readme:

/* Importing Library */
var SouthernCompanyAPI = require("southern-company-api").SouthernCompanyAPI;

/* Instantiating API */
const SouthernCompany = new SouthernCompanyAPI({
  username: "REDACTED",
  password: "REDACTED",
  accounts: ["REDACTED"],
});

/* Listening for login success */
SouthernCompany.on("connected", () => {
  console.info("Connected...");

  async function fetchMonthly() {
    /* Getting Monthly Data */
    const monthlyData = await SouthernCompany.getMonthlyData();

    /* Printing Monthly Data */
    console.info("Monthly Data", JSON.stringify(monthlyData));
  }
  fetchMonthly();

  async function fetchDaily() {
    /* Getting Daily Data */
    const startDate = new Date(2020, 2, 1);
    const endDate = new Date();
    const dailyData = await SouthernCompany.getDailyData(startDate, endDate);

    /* Printing daily data */
    console.info("Daily Data", JSON.stringify(dailyData));
  }
  fetchDaily();
});

/* Listening for any errors */
SouthernCompany.on("error", console.error);

And I got the following exception:

TypeError: Cannot read properties of undefined (reading '1')
    at SouthernCompanyAPI.<anonymous> (REDACTED/node_modules/southern-company-api/dist/main.js:356:48)
    at Generator.next (<anonymous>)
    at fulfilled (REDACTED/node_modules/southern-company-api/dist/main.js:5:58)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)

I popped into main.js and did some good 'ol console.log debugging.

I added:

console.log("rawUsageData",  date_fns_1.differenceInCalendarDays(endDate, startDate), rawUsageData)

right before the offending line and got the following output:

rawUsageData 709 [ [], [] ]

I then took a look at what graphData had in it. Looked like:

graphData [ { labels: [ [Object] ] }, { labels: [ [Object] ] } ]

Here's what the raw graphset data looks like:

{"graphset":[{"labels":[{"text":"No Bill History Available","visible":true,"font-family":"Open Sans","y":0,"x":0,"text-align":"center","font-size":"18","font-color":" #DA312A","font-weight":"300"}]}],"gui":{"behaviors":[{"id":"ShowAll","enabled":"none"},{"id":"ViewSource","enabled":"none"},{"id":"3D","enabled":"none"},{"id":"BugReport","enabled":"none"},{"id":"About","enabled":"none"},{"id":"FullScreen","enabled":"none"},{"id":"Print","enabled":"none"},{"id":"Reload","enabled":"none"},{"id":"LogScale","enabled":"none"},{"id":"SaveAsImage","enabled":"none"},{"id":"DownloadPDF","enabled":"none"},{"id":"DownloadSVG","enabled":"none"}],"context-menu[mobile]":{"button":{"visible":false,"background-color":"transparent","alpha":0,"border-radius":0},"gear":{"visible":false,"alpha":0}}}}

No series property as the code expects. The code checks for this with a if data.series != null however series is undefined here, not null. Additionally, the code is expecting 709 data points.

I was just going to fix this with a simple if series is truthy but that's not enough. What do we want to about the 708 missing data points? Do we want to throw an error if we see No Bill History Available?

Note: If I change the example code to be something slightly more reasonable it works as expected:

    const endDate = new Date();
    const startDate = new Date();
    startDate.setDate(startDate.getDate() - 7); // 7 days
    const dailyData = await SouthernCompany.getDailyData(startDate, endDate);
@apearson
Copy link
Owner

Seems like if you ask for too much data then the API returns nothing to you. Does that sound right?

If so maybe we clamp the date range and throw an error if you're over the limit

@apearson apearson closed this as completed Jan 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants