Update date of all the object when using find()

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

Why not formatting the date when rendering it to the actual page !
Plus you didn’t mention where you are implementing this .
What’s the tech stack.

1 Like

Thanks. I just editted my post.

You can’t format the date in query to my knowledge. Just format the date when you are sending them.
create a variable object holding and formatting the values you want to send. then send that object variable.

const result = {
  _id: user._id,
  username: user.username,
  count: exercise.length,
  date : formatDate(excercise.date)
  .....
}
res.json(result);

Something like this; where formatDate is a pure function that takes a date value as argument and returns the date in your desired format.

1 Like

How do I do that? I’m actually returning an array right? If it’s an object I could easily do it. What do I need to modify in exercise function? Can you elaborate? Or link to any resource? Thanks.

which function is returning what type of data ; It’s not clear to me. Since i can’t see the actual data . Is your code on github ?

what type of data is getExerciselogs returning ? show some actual data

I have sent you the link…

getExerciseLogs returns the array of all the exercise’s of users…