eoja
eoja

Reputation: 1922

How to format a date that is a value in an array of objects

I have an array of objects:

var exerciseLog = [{“date”:“2019-07-02T21:18:48.946Z”,“description”:“pull ups”,“duration”:“90”},{“date”:“2019-07-02T21:22:30.395Z”,“description”:“push ups”,“duration”:“90”},{“date”:“2019-07-02T22:19:37.790Z”,“description”:“push ups”,“duration”:“50”}]

I want to format the date to exclude the time so the date is either displayed as "YYYY-MM-DD" or "YYYY/MM/DD".

I've tried map, forEach, slice, splice.

exerciseLog = exerciseLog.forEach(x => x.date.toLocaleDateString());

Doesn't work in the relevant portion of the code:

app.get("/api/exercise/log", function (req, res) {
    var userId = req.query.userId; 
    var from = req.query.from ? new Date(req.query.from) : new Date("1970-01-01");
    var to = req.query.to ? new Date(req.query.to) : new Date();

    User.findById(userId, function (err, doc) {
      if (!doc) {
        res.send({ "error": "userId not found" });
      } else {
        var exerciseLog = doc.exercises.sort((a, b) => a.date.getTime() - b.date.getTime())
          .filter(x => x.date >= from && x.date <= to);
        var limit = !isNaN(req.query.limit) ? req.query.limit : exerciseLog.length;
        exerciseLog = exerciseLog.slice(0, limit);
        exerciseLog = exerciseLog.forEach(x => x.date.toLocaleDateString());
        res.send({ "username": doc.username, "Exercise Count": exerciseLog.length, "Exercise Log": exerciseLog });
      }
    });
  });

The error:

events.js:160
6:59 PM
      throw er; // Unhandled 'error' event
6:59 PM
      ^
6:59 PM
6:59 PM
TypeError: Cannot read property 'length' of undefined
6:59 PM
Jump to
at /app/server.js:138:77
6:59 PM
    at /rbd/pnpm-volume/52232b84-c31b-4266-9261-f25b6365dff7/node_modules/.registry.npmjs.org/mongoose/5.6.2/node_modules/mongoose/lib/model.js:4846:16
6:59 PM
    at /rbd/pnpm-volume/52232b84-c31b-4266-9261-f25b6365dff7/node_modules/.registry.npmjs.org/mongoose/5.6.2/node_modules/mongoose/lib/query.js:4283:12
6:59 PM
    at process.nextTick (/rbd/pnpm-volume/52232b84-c31b-4266-9261-f25b6365dff7/node_modules/.registry.npmjs.org/mongoose/5.6.2/node_modules/mongoose/lib/query.js:2776:28)
6:59 PM
    at _combinedTickCallback (internal/process/next_tick.js:73:7)
6:59 PM
    at process._tickCallback (internal/process/next_tick.js:104:9)

Which points to this line:

var limit = !isNaN(req.query.limit) ? req.query.limit : exerciseLog.length;

but if I remove the code with the forEach line I have no errors.

Full code https://glitch.com/edit/#!/swamp-liquid?path=server.js:138:53.

Upvotes: 5

Views: 9080

Answers (3)

eoja
eoja

Reputation: 1922

@Miles Grover and @BlueWater86 thank you for the help. I tried map before and it didn't work but now it does.

exerciseLog = exerciseLog.map(x => x.date.toLocaleDateString());

returned only the formatted dates, so I had to do this to keep the rest of the object info:

exerciseLog = exerciseLog.map(x => "description: " + x.description + ", duration: " + x.duration + ", date: " + x.date.toLocaleDateString());

Upvotes: 2

Miles Grover
Miles Grover

Reputation: 609

exerciseLog = exerciseLog.forEach(x => x.date.toLocaleDateString());

forEach() doesn't return anything, so you're setting exerciseLog to null or undefined. If you use map() instead, that line will set exerciseLog to a new array based on what's returned in the function you pass to map().

The next issue is that the quotes in your original code snippet are curly quotes -- not sure where that's coming from, but nothing is going to work until those are single or double straight quotes.

I think the last issue is that x.date is already a date string, not a Date object, so toLocaleDateString() won't work on it. You may be able to just use x.date, or if you do need to convert the date to a different locale, you'd do something like new Date(x.date).toLocaleDateString().

Upvotes: 0

BlueWater86
BlueWater86

Reputation: 1817

Your bug is because you are assigning the return value from Array.prototype.forEach to exerciseLog.

Array.prototype.forEach does not return anything.

You want to use Array.prototype.map.

exerciseLog = exerciseLog.map(x => x.date.toLocaleDateString());

Upvotes: 4

Related Questions