I searched and came to know that code is executed after res.send() but in my case its executing only for date
app.post("/api/exercise/add", (req, res) => {
const userId = req.body.userId;
const description = req.body.description;
const duration = req.body.duration;
let date;
if (!userId) {
res.send("enter userID");
}
if (!description) {
res.send("enter description");
}
if (!duration) {
res.send("enter duration");
}
if (req.body.date) {
date = new Date(req.body.date);
if (!(date instanceof Date && !isNaN(date))) {
res.send("enter valid date");
}
} else {
date = new Date();
}
date = date.toString().substring(0, 24);
userInfo.findById(userId, function(err, user) {
if (err) throw err;
user.log.push({
description: description,
duration: duration,
date: date
});
user.save(function(err, user) {
if (err) console.error(err);
});
});
});
here:-
the rest of the code is not getting executed and nothing is saved in db
if (!userId) {
res.send("enter userID");
}
if (!description) {
res.send("enter description");
}
if (!duration) {
res.send("enter duration");
}
but if the user enters invalid date its getting stored
{"_id":{"$oid":"5f1a343be7028b14397806a5"},"
count":{"$numberInt":"0"},"username":"xrahulx",
"log":[{"description":"hhh","duration":{"$numberInt":"147"},
"_id":{"$oid":"5f1a3451e7028b14397806a6"},
"date":"Invalid Date"},
{"description":"hhh",
"duration":{"$numberInt":"147"},
"_id":{"$oid":"5f1a35178a3e3515f03e8c69"},
"date":"Invalid Date"}]
}