0

This code is on my nodeJS server. Every time the server gets to it, it crashes. Don't know what's wrong... Could someone help me?

dateArray[11], dateArray[12] and dateArray[13] are either empty or are Date values.

var dateArray11 = null;
var tempDateValue = "";
if ((typeof dateArray[11] !== "undefined") || (typeof dateArray[12] !== "undefined") || (typeof dateArray[13] !== "undefined")) {
    dateArray11 = new Date(Math.min((dateArray[11].getTime()), (dateArray[12].getTime()), (dateArray[13].getTime())));
    if (dateArray11.getTime() === dateArray[11].getTime()) {
        tempDateValue = 11;
    }
    if (dateArray11.getTime() === dateArray[12].getTime()) {
        tempDateValue = 12;
    }
    if (dateArray11.getTime() === dateArray[13].getTime()) {
        tempDateValue = 13;
    }
}
Termininja
  • 6,620
  • 12
  • 48
  • 49
Emilio
  • 1,314
  • 2
  • 15
  • 39
  • You could make sure they're date objects by using `instanceof Date`. – Ben Fortune Sep 09 '16 at 15:44
  • That's what I was trying to do with (typeof Date !== "undefined"). Is it wrong? Didn't have any issues with it till now... – Emilio Sep 09 '16 at 15:47
  • 2
    Shouldn't the condition be `&&` instead of `||` – Karan Punamiya Sep 09 '16 at 15:52
  • 1
    not really, I want it to be "if at least one of them exists, take the lowest date of the ones that exist, then define the corresponding tempDateValue" But maybe I can't use .getTime() with values that don't exist. Maybe it is the issue!? – Emilio Sep 09 '16 at 15:58
  • Thanks @KaranPunamiya I think we found the issue. – Emilio Sep 09 '16 at 16:15
  • Saying the server "crashes" means something different from the server "issues a run-time error". –  Sep 09 '16 at 19:09
  • @torazaburo Thanks for letting me know! I will use the appropriate terminology next time! – Emilio Sep 09 '16 at 19:42

2 Answers2

0

typeof(new Date()) is always give you an object and you are comparing it with String since typeof("undefined") is String.

console.log(typeof 123)// return number

console.log(typeof new Date())// return object

console.log(typeof 'hola')//return string

The typeof operator returns type information as a string. There are six possible values that typeof returns: "number," "string," "boolean," "object," "function," and "undefined." The parentheses are optional in the typeof syntax.

abdulbarik
  • 6,101
  • 5
  • 38
  • 59
0

Since you said that dateArray[11], dateArray[12] and dateArray[13] are either empty or are Date values.

You cannot call a getTime() function of an empty or undefined value.

Before you call it, you should check if dateArray[11], dateArray[12] and dateArray[13] are Date Objects. You can see how to check that in this answer: https://stackoverflow.com/a/643827/3442014


EDIT: This is my aproach of what you're doing:

var dateArray=[];
dateArray[11]=new Date("2011/06/31");
dateArray[12]=new Date("2011/06/26");
dateArray[13]="";

var tempDateValue = "";

var dates=[];
if (dateArray[11] instanceof Date) dates.push(dateArray[11]);
if (dateArray[12] instanceof Date) dates.push(dateArray[12]);
if (dateArray[13] instanceof Date) dates.push(dateArray[13]);

var min = new Date(Math.min.apply(null,dates));

var tempDateValue = dates.map(Number).indexOf(+min) + 11;
Community
  • 1
  • 1
Federicorp
  • 76
  • 5
  • it'll not give object type of Date it just give you object even if you check typeof null, it will give you `object` :) – abdulbarik Sep 09 '16 at 16:18
  • @Federicorp As I said, I want it to be : "if at least one of them exists, take the lowest date of the ones that exist, then define the corresponding tempDateValue" . So you're right, this is the issue, but I don't want to check if dateArray[11], dateArray[12] AND dateArray[13] are Date Objects, because not all of them are. I will have to find another way to make it work. – Emilio Sep 09 '16 at 16:20
  • Thanks for your elaborate answer! In the meantime I figured it out. The part I was missing was that I didn't need to .getTime() date values in an array before I could Math.min.apply(null, the array. – Emilio Sep 09 '16 at 19:57