Express: code executing after res.send()

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"}]
}

1 Try use elseif block rather than only if
2 After every unsuccessful request after res.send() try ‘return’ statement. otherwise whole program get executed.

but why is it not getting executed only if I dont enter description , duration ,id ?

Sorry. But can u state the above problem again. I think i did’t understand clearly.

yes, as you can see in the code if the uses does not enters userid, description and duration I do res.send() and the rest of the code is not executed . same should happen when user enters date but this time rest of the code is getting executed and invalid date is getting saved in the db

In the schema did you correctly specify the date
i. e data type the date variable receives should instance of Date. Because at end you are converting date to string and not a Date type.

it does not matter , what I’m saying is why the code for saving is getting executed even after invalid date but not when I dont enter userid, description and duration

because even when date is invalid this line executed

    date = new Date();
  }


At the end date is the current date. Then you converting it to string and trying to save.

this executes when the user has not entered date,but the point is not that

So you were saying even when user enter a string in place of date it still storing?

it does not even matters , if date is correct incorrect etc

Give me an example of invalid date that you are entering

are you not reading ??

sorry for the misunderstanding :sweat_smile:. try this. this might resolve the issue. keep in mind that js convert integer string to Number whwn required without throwing any error.

else {
      date = new Date();
      res.send("enter date");
    }

if i am still not getting it let me know.

:sob: :sob:

this is the issue

plz try ‘return’ statement after every res.send() to stop executing further

I have solved the issue but I want to know why this code is working like this

i did try the code in my console. for me saving is executed even when any one of above four is not entered(default behaviour).

hmm leave it, thanks for time