0

I have an array which i need to pass over to a php page, the array is fine, but i have to include a new variable into it, variable is lastBookingDate, then this variable i match with current date and get the month diff so far is good, the issue is when the varible is undefined i got the error

Uncaught TypeError: Cannot read property 'split' of undefined at HTMLDocument. (:2:170)"

I have an if statement but I keep getting the same error. The variable info is coming from a variable in datalayer(GTM) that when there no info the variable doesn't exist at all

var bookingDateExist = GTM;

if (bookingDateExist === "undefined") {
  var Booking = "no booking";
} else {
  var lastBookedDate = bookingDateExist;
  var dateString = lastBookedDate;
  var dateParts = dateString.split("/");
  var dateObject = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);

  var start_date = new Date(); //Create start date object by passing appropiate argument
  var end_date = dateObject;
  var total_months = (end_date.getFullYear() - start_date.getFullYear()) * 12 + (end_date.getMonth() - start_date.getMonth());
  var Booking = Math.abs(total_months);
}

I expected variable Booking = no booking or variable booking = 12 (amount of month diff)

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
walter alexander
  • 179
  • 2
  • 18
  • 1
    do you have really a sting in `bookingDateExist` or just the value `undefined` (without quotes). – Nina Scholz May 24 '19 at 19:43
  • You can literally just do `if (bookingDateExist)` – Isaac Vidrine May 24 '19 at 19:49
  • 2
    @IsaacVidrine It has to be `if (!bookingDateExist)` in OP's case. – connexo May 24 '19 at 19:50
  • without quotes, since is a varibale that when is empty not listed , therefor it become undefined – walter alexander May 24 '19 at 19:50
  • 1
    Maybe you meant `=== undefined` and not `=== "undefined"`? Also, it is recommended to declare variables outside of the if else statement to avoid confusion https://stackoverflow.com/questions/6964895/javascript-variable-scope-in-the-if-statement , and to name them in lower case (I'm talking about `var Booking`). – user44 May 24 '19 at 19:52

2 Answers2

4

The issue is because you're checking to see if the bookingDateExist variable is a string with the value of undefined.

To fix this you can use type coercion to convert the value to a boolean. Because null will coerce to false, you can simply use !bookingDateExist in the condition instead, like this:

if (!bookingDateExist) {
  var Booking = "no booking";
} else {
  var dateParts = bookingDateExist.split("/");
  var end_date = new Date(dateParts[2], dateParts[1] - 1, dateParts[0]);

  var start_date = new Date();
  var total_months = (end_date.getFullYear() - start_date.getFullYear()) * 12 + (end_date.getMonth() - start_date.getMonth());
  var Booking = Math.abs(total_months);
}

The only caveat to this is if the value you coerce would ever contain a valid value which equates to false, such as 0, or 'false'. However in this case it's a date so that should never happen.

Also note that I tidied up the logic in the else condition; you were creating a lot of unnecessary variables.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
2

if (bookingDateExist === "undefined")

This will not work, because you are actually comparing the variable to the string literal "undefined" - literally the word itself.

You could make this actually check for an undefined value by doing this:

if (typeof bookingDateExist === "undefined")

However, this will not help if you encounter a NULL rather than undefined. Instead, I would suggest:

if (bookingDateExist == null)

Using the loose comparison == will coerce undefined types to be considered equal to null. This will check if bookingDateExist is null OR undefined.

Brad S
  • 186
  • 4