护照中的Date.now无法完成

时间:2016-07-19 06:36:21

标签: javascript node.js mongodb mongoose passport.js

我想在我的nodejs网站上通过facebook创建用户时分配registerdate。我使用护照。 如果我使用本地提供程序,则可以在我的Mongoose Schema中使用

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

然而在护照facebook中这是我的代码:

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));
  }));

然后继续它在页面中返回错误。

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)

如何通过护照正确调用Date.now功能? 谢谢。

1 个答案:

答案 0 :(得分:5)

Date.now是一个函数,你应该这样使用它:Date.now()如果你想得到一个时间戳