问题
I'm trying to add last login date in my application.But I can't able to achieve it.The current date and last login date are same in my code....please some one help me......enter image description here [
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const schema = new Schema({
username: {
type: String,
unique: true,
required: true
},
email:{
type:String,
unique:true,
required:true
},
hash: {
type: String,
required: true
},
defaultAccountId:{
type:Number,
required:true
},
buisnessUnit:{
type:Array,
required:true
},
createdDate: {
type: Date,
default: Date.now
},
lastLoginDate: {
type: Date,
default: Date.now
}
});
schema.statics.login = function login(id, callback) {
return this.findByIdAndUpdate(id, { $set : { 'lastLoginDate' : Date.now() }, new : true }, callback);
};
schema.set('toJSON', {
virtuals: true
});
module.exports = mongoose.model('User', schema);
]1
回答1:
Change your part of code to this and you are done.
schema.statics.login = function login(id, callback) {
return this.findByIdAndUpdate(id,{'$set' : { 'lastLoginDate' : Date.now()} }, { new : true }, callback);
};
来源:https://stackoverflow.com/questions/59859512/how-to-add-last-login-date-in-application-using-nodejs-and-mongodb