Skip to content

Commit

Permalink
Merge pull request #227 from gauravsinghaec/master
Browse files Browse the repository at this point in the history
Use moment.js to format date while saving and reading dates in MongoDB
  • Loading branch information
gauravsinghaec authored May 12, 2018
2 parents 369a2c8 + 763625d commit 26c5790
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
11 changes: 6 additions & 5 deletions controllers/userController.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -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
},
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 1 addition & 2 deletions routes/main/donate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
});
Expand Down
3 changes: 1 addition & 2 deletions routes/profile/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
});

Expand Down

0 comments on commit 26c5790

Please sign in to comment.