1

I don't know what's going on here... but apparently TS is not recognizing Date as Date, instead it thinks it is a string.

Here's the code I use to save the Date:

var validUntil = new Date();

validUntil.setSeconds(validUntil.getSeconds() + tokenResponse.expires_in);
tokenResponse.valid_until = validUntil;

And this is my TokenResponse class:

export class TokenResponse {
    access_token: string;
    token_type: string;
    expires_in: number;
    valid_until: Date;
    error: string;
    error_description: string;
}

And here is where the exception is thrown:

userToken && userToken.valid_until.getTime()  >= new Date().getTime()

Here's the exception:

enter image description here

Any ideas on what's wrong?

Thanks!

EDIT

By the way, I don't think it's related to Date variable works, but functions on it do not since I'm creating a new Date

Community
  • 1
  • 1
eestein
  • 4,914
  • 8
  • 54
  • 93
  • Post a complete minimal example reproducing the problem. We have no idea what userToken is, among other things. – JB Nizet Nov 11 '16 at 16:50
  • @JBNizet it comes from my API. `userToken == TokenResponse` – eestein Nov 11 '16 at 16:53
  • If it comes from your API, I guess it's JSON, and JSON doesn't have Dates. Only string, number, boolean. And a JSON object will never be an instance of any class. Again, a complete example would help. – JB Nizet Nov 11 '16 at 16:54
  • @JBNizet I thought the information there would be enough. Is there anything specific that would help? I receive the response from the server (1st piece of code), my class is `TokenResponse` and when I try to use it it's not loading as Date (apparently) – eestein Nov 11 '16 at 16:55
  • @JBNizet you are correct about JS, though I'm creating a new instance of Date. I'm not relying on the response. – eestein Nov 11 '16 at 16:57
  • Well, then you have your answer. An object created by parsing JSON is a plain old object, not an instance of any class. And it doesn't contain Date, because JSON doesn't have support for a Date type. This is still jus conjecture, since you won't post the code that shows how userToken is created. – JB Nizet Nov 11 '16 at 16:57
  • @JBNizet Well, I appreciate your time trying to help. As I said I thought the **important** part was there, it seems like it wasn't. According to David below the problem seems to be related to saving the information locally - information I forgot to provide. Anyway, to answer your question, tokenResponse/userToken is a ASP.NET WebApi response to a login request. All I do is create the `Date` type and save it locally. – eestein Nov 11 '16 at 17:04

1 Answers1

2

In code that isn't posted here, the value of userToken.validUntil is being set as a string. You will see that if you check typeof userToken.validUntil before the part of the code where calling getTime() errors.

Make sure when setting userToken.validUntil that you always assign a Date object to it. If you're deserializing JSON to userToken, then make sure the deserializer converts date strings to date objects or for a quick fix do (though not recommended):

userToken.validUntil = new Date(userToken.validUntil as any);

Note the following:

var o = { d: new Date() };
localStorage.setItem("test", JSON.stringify(o));
o = JSON.parse(localStorage.getItem("test"));
typeof o.d === "string"; // true
David Sherret
  • 101,669
  • 28
  • 188
  • 178
  • Thanks David, the only time I set `validUntil` is there :/ The only thing I can think of is maybe the local storage is changing the type? – eestein Nov 11 '16 at 16:57
  • I just checked your update. Isn't that the same as what I was doing? `var validUntil = new Date(); validUntil.setSeconds(validUntil.getSeconds() + tokenResponse.expires_in); tokenResponse.valid_until = validUntil;` – eestein Nov 11 '16 at 17:00
  • Ok... so the problem is related to the storage... is there a way to force it to save `Date`? – eestein Nov 11 '16 at 17:01