heygema heygema - 4 months ago 33
Node.js Question

Date.now in passport.js cannot be done

I want to assign registerdate when I create a user via facebook in my nodejs website. I'm using passport.
If I use local provider it is possible in my Mongoose Schema to use

regisDate: {
type:Date,
'default': Date.now
},


however in passport facebook this is my code:

function(accessToken, refreshToken, profile, done) {
User.findOne({'facebook.id': profile.id}).exec()
.then(user => {
if (user) {
return done(null, user);
}

user = new User({

firstName: profile.first_name,
lastName: profile.last_name,
address: '',
regisDate: Date.now,
birthday: profile.birthday,

email: profile.emails[0].value,
role: 'user',
provider: 'facebook',
facebook: profile._json
});
user.save()
.then(user => done(null, user))
.catch(err => done(err));
})
.catch(err => done(err));
}));


and when proceed it returning an error in page like this.

ValidationError: CastError: Cast to Date failed for value "function now() { [native code] }" at path "regisDate"
at MongooseError.ValidationError (/home/gema/Desktop/stash/tx/node_modules/mongoose/lib/error/validation.js:22:11)
at model.Document.invalidate (/home/gema/Desktop/stash/tx/node_modules/mongoose/lib/document.js:1411:32)
at model.Document.set (/home/gema/Desktop/stash/tx/node_modules/mongoose/lib/document.js:696:10)
at model.Document.set (/home/gema/Desktop/stash/tx/node_modules/mongoose/lib/document.js:541:18)
at model.Document (/home/gema/Desktop/stash/tx/node_modules/mongoose/lib/document.js:67:10)
at model.Model (/home/gema/Desktop/stash/tx/node_modules/mongoose/lib/model.js:41:12)
at new model (/home/gema/Desktop/stash/tx/node_modules/mongoose/lib/model.js:3200:11)
at passport.js:24:16
From previous event:
at model.wrappedPointCut [as save] (/home/gema/Desktop/stash/tx/node_modules/mongoose/lib/document.js:1893:14)
at passport.js:37:14
at tryOnImmediate (timers.js:534:15)
at processImmediate [as _immediateCallback] (timers.js:514:5)
From previous event:
at Strategy._verify (passport.js:19:8)
at /home/gema/Desktop/stash/tx/node_modules/passport-oauth2/lib/strategy.js:193:24
at /home/gema/Desktop/stash/tx/node_modules/passport-facebook/lib/strategy.js:181:5
at passBackControl (/home/gema/Desktop/stash/tx/node_modules/oauth/lib/oauth2.js:125:9)
at IncomingMessage.<anonymous> (/home/gema/Desktop/stash/tx/node_modules/oauth/lib/oauth2.js:143:7)
at emitNone (events.js:85:20)
at IncomingMessage.emit (events.js:179:7)
at endReadableNT (_stream_readable.js:913:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)


How to call Date.now function properly via passport ?
thank you.

Answer

Date.now is a function, you should use it this way: Date.now() if you want to get a timestamp