简体   繁体   English

如何使用nodejs和mongodb在应用程序中添加上次登录日期?

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM