I need to update a field in one of my model when querying. I’m using Model.find ()which returns me an array of all the exercise’s of the users. But the date format inside my schema is always in ISO . Which I want to change.
My exercise schema looks like this.
var ExerciseSchema = new Schema({
userid: {
type: String,
required: true
},
description: String,
duration: Number,
date: {
type: Date,
default: Date.now
}
});
here is how I try to call the get all exercise function…
async function getExerciselogs(userId) {
try {
var exerciseLog = await ExerciseModel.find(
{ userid: userId },
"description duration date -_id"
);
return exerciseLog;
} catch (err) {
throw err;
}
}
what I want is to format the date retrieved into new Date().toDateString() format or “Thu Jan 16 1997” this format. How can I do that?
Thanks for your help.
app.get("/api/exercise/log", (req, res) => {
if (Object.keys(req.query).length === 0) {
console.log("inside all the users");
UserModel.find({}, (err, users) => {
if (err) res.send({ error: err });
res.json({
users
});
});
} else {
var userDetail = getUserDetail(req);
console.log("inside from to");
userDetail
.then(user => {
getExerciselogs(user._id)
.then(exercise => {
res.json({
_id: user._id,
username: user.username,
count: exercise.length,
log: exercise
});
})
.catch(err => {
res.send(err);
});
})
.catch(err => {
res.send(err);
});
}
});
That’s how I’m getting my tech stack