how to add last login date in application using nodejs and mongodb?

匆匆过客 提交于 2021-02-11 15:36:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!