ace23
ace23

Reputation: 142

Cannot read property 'length' error in JS

I have the an array of date range and I need to get a difference between number of days in between those months and create an array.

10/05/2015 - 11/05/2015 = 30 days
11/05/2015 - 12/ 05/2015 = 31 days
[30,31....]

I have the following code for date range.

function createLedger(stDate, etDate) {

  if (stDate && etDate) {
    var endOfLeaseDate = moment(etDate, "MM/DD/YYYY");
    var startOfLeaseDate = moment(stDate, "MM/DD/YYYY");
    dateRange(startOfLeaseDate, endOfLeaseDate);
  }
}

function dateRange(stDate, etDate) {
  var dates = [];

  var now = stDate.clone();
  var day = stDate.date();

  while (now.isBefore(etDate)) {
    //deal with variable month end days
    var monthEnd = now.clone().endOf("month");
    if (now.date() < day && day <= monthEnd.date()) {
      now.date(day);
    }

    dates.push(now.format("MM/DD/YYYY"));
    now = now.clone().add({
      "months": 1
    });
  }
  return dates;
}

function daysBetween(date1, date2) {
  var Diff = Math.abs(date2.getTime() - date1.getTime());
  var TimeDifference = Math.round(Diff / (1000 * 3600 * 24));
  return TimeDifference;
}

function RunLedgerAndPV() {
  var pDate = "11/21/2018"
  var stDate = "10/5/2015";
  var etDate = "12/4/2019";
  var dateArr = createLedger(stDate, etDate);
   
  var dayCounts = "";
  for (var x = 0; x < dateArr.length; x++) {
    dayCounts += daysBetween(dateArr[x], dateArr[x + 1], ",");
  }
  console.log(dayCounts);
}
RunLedgerAndPV();
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

It's throwing an error at dateArr.length for some reason. Not sure what i am doing wrong here. Any help is appreciated. Thank you.

Upvotes: 0

Views: 80

Answers (2)

Darren Keen.
Darren Keen.

Reputation: 532

To add on to Vijay you are not returning anything in dateRange() either. Return dates array then return where you called dateRange().

Be aware this leads to more errors to do with your naming of daysBetween function when you are calling daysBetweenArrears.

EDIT Few other errors: You are calling getTime() on a string so this causes an error. You need to convert to a date object using new Date(date2) - new Date(date1).

Another return also missing for the Run function.

Code below:

function createLedger(stDate, etDate) {
  if (stDate && etDate) {

    var endOfLeaseDate = moment(etDate, "MM/DD/YYYY");
    var startOfLeaseDate = moment(stDate, "MM/DD/YYYY");
    return dateRange(startOfLeaseDate, endOfLeaseDate); // Added return
  }
}

function dateRange(stDate, etDate) {
  var dates = [];

  var now = stDate.clone();
  var day = stDate.date();

  while (now.isBefore(etDate)) {
    //deal with variable month end days
    var monthEnd = now.clone().endOf("month");
    if (now.date() < day && day <= monthEnd.date()) {
      now.date(day);
    }

    dates.push(now.format("MM/DD/YYYY"));
    now = now.clone().add({
      "months": 1
    });
  }
  return dates; // Added return

}

function daysBetween(date1, date2) {
  var Diff = Math.abs(new Date(date2).getTime() - new Date(date1).getTime()); // Used new Date()
  var TimeDifference = Math.round(Diff / (1000 * 3600 * 24));
  return TimeDifference;
}

function RunLedgerAndPV() {
  var pDate = "11/21/2018"
  var stDate = "10/5/2015";
  var etDate = "12/4/2019";
  var dateArr = createLedger(stDate, etDate);


  var dayCounts = "";
  for (var x = 0; x < dateArr.length; x++) {
    dayCounts += daysBetween(dateArr[x], dateArr[x + 1]) + ' '; // added a + ' ' to add a space to the result. Removed the ',' that you were adding in daysBetween but not using
  }
  return dayCounts; // Added return
}

RunLedgerAndPV(); //This wont show anything so wrap it in a console.log to see it return what you need

Upvotes: 1

Vijay Singh
Vijay Singh

Reputation: 132

In your function "crrateLedger" you don't return anyting and you are assigning that in "var dateArr" hence it is set to undefined by javascript and you are trying to access property length of dateArr which is undefined

Upvotes: 1

Related Questions