From 763625d71c1f7e2f3f301bb96694d13b33fee4e9 Mon Sep 17 00:00:00 2001 From: gauravsinghaec Date: Sat, 12 May 2018 19:45:37 +0530 Subject: [PATCH] Use moment.js to format date while saving and reading. --- controllers/userController.js | 11 ++++++----- routes/main/donate.js | 3 +-- routes/profile/users.js | 3 +-- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/controllers/userController.js b/controllers/userController.js index 646fd36d..bd0fa8a5 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -1,5 +1,6 @@ const UserAcct = require('../models/user'); const bcrypt = require('bcryptjs'); +var moment = require('moment'); var signupUser = function(usrObj, callback) { var newUser = new UserAcct(usrObj); @@ -175,7 +176,7 @@ var updateUser = function(userObj, callback) { age: userObj.age, blood_grp: userObj.bloodgroup, gender: userObj.gender, - last_donation: userObj.last_donation, + last_donation: moment(userObj.last_donation,'DD/MM/YYYY').format('MM/DD/YYYY'), height: userObj.height, weight: userObj.weight }, @@ -211,12 +212,12 @@ var updateUser = function(userObj, callback) { }); } -var bookAppointment = function(userObj, callback) { - var item ={"indiv.appointment.appointment_date": userObj.bookdate, +var bookAppointment = function(userObj, callback) { + var item ={"indiv.appointment.appointment_date": moment(userObj.bookdate,'DD/MM/YYYY').format('MM/DD/YYYY'), "indiv.appointment.donor_city" : userObj.bookcity, - updated : Date.now() + "updated" : Date.now() }; - UserAcct.findByIdAndUpdate(userObj.id, { $set: {item} }, function(err, result) { + UserAcct.findByIdAndUpdate(userObj.id, { $set: item }, function(err, result) { if (err) { callback(err, undefined); } else if (result) { diff --git a/routes/main/donate.js b/routes/main/donate.js index 3605ee98..1f90998b 100644 --- a/routes/main/donate.js +++ b/routes/main/donate.js @@ -19,14 +19,13 @@ router.post('/', let userObj = req.body; userObj.title = 'Donate Blood'; userObj.id = req.user._id; - console.log(userObj); bookAppointment(userObj, function(err, result) { if (err) { userObj.alertMessage = "DB Error:"+err.message; res.render('main/donate', userObj); } else if (result) { req.flash('successMessage', 'Appointment booked successfully.'); - res.redirect('/'); + res.redirect('/donate'); } }); }); diff --git a/routes/profile/users.js b/routes/profile/users.js index e3750382..d9d07e80 100644 --- a/routes/profile/users.js +++ b/routes/profile/users.js @@ -15,7 +15,7 @@ router.get('/', login_required, function(req, res, next) { obj.age = req.user.indiv.age; obj.height = req.user.indiv.height; obj.weight = req.user.indiv.weight; - obj.last_donation = moment(req.user.indiv.last_donation).format("L"); + obj.last_donation = moment(req.user.indiv.last_donation,'MM/DD/YYYY').format('DD/MM/YYYY'); obj.orgname = req.user.non_indiv.org_name; obj.license = req.user.non_indiv.license; obj.stock = req.user.non_indiv.unit_stock; @@ -28,7 +28,6 @@ router.get('/', login_required, function(req, res, next) { obj.city = req.user.address.city; obj.state = req.user.address.state; obj.zip = req.user.address.pincode; - console.log(moment(obj.last_donation).format("L")); res.render('profile/users', obj); });