I have set up nodejs with express, sessions, mongoose etc. Login and navigation works all fine, now I just want to append additional information to the user, specifically a date object.
Here is how I attempted to do that:
var user = req.body.user;
if(user) {
if(!user.viewdate)
user.viewdate = new Date();
}
Note - I removed a lot of my code to highlight the problem at hand.
Now for some reason when I do that, Nodejs tells me that user.viewdate
is actually a string and not a Date object, which means that I can't access any of the date functions.
console.log(typeof(user.viewdate) + ", " + user.viewdate);
// prints: string, 2017-05-23T13:37:57.672Z
user.viewdate.getDate();
// TypeError: user.viewdate.getDate is not a function
I have actually solved the problem by converting the string to a date object, editing it, and then converting it to a string again.
var date = new Date(user.viewdate);
date.setDate(date.getDate() + 1);
user.viewdate = date;
But all this converting seems tedious and unnecessary when I could just access the actual date.
Can someone tell me, if it's possible to save a Date attribute to a user in a session?
via Felix Jassler
No comments:
Post a Comment