Trouble with reqired response to log. What is going on here? Please?

Tell us what’s happening:

I’m having alot of trouble getting part of this to pass tests.
although it passes the last test when the queries are added. I can find no difference to what’s returned and the example or the output
from the example programs.

Here’s whats happening while.it runs the tests


post /api/users/:_id/logs req.body=  {}
post /api/users/:_id/logs req.params=  { _id: '619062c982800169b43aa4c0' }
from:  1989-12-31T00:00:00.000Z to:  1990-01-03T23:59:59.000Z
Returning : {
  username: 'fcc_test_16368524256',
  count: 2,
  _id: '619062c982800169b43aa4c0',
  log: [
    { description: 'test', duration: 60, date: 'Tue Jan 02 1990' },
    { description: 'test', duration: 60, date: 'Mon Jan 01 1990' }
  ]
}
post /api/users/:_id/logs req.body=  {}
post /api/users/:_id/logs req.params=  { _id: '619062c982800169b43aa4c0' }
from:  1970-01-01T00:00:00.000Z to:  2021-11-14T23:59:59.999Z
Returning : {
  username: 'fcc_test_16368524256',
  count: 1,
  _id: '619062c982800169b43aa4c0',
  log: [ { description: 'test', duration: 60, date: 'Mon Jan 01 1990' } ]
}

this is the code


app.get('/api/users/:_id/logs', async (req, res) => {
  console.log("post /api/users/:_id/logs req.body= ", req.body);
  console.log("post /api/users/:_id/logs req.params= ", req.params);
  try {
    const user = await User.findById({ _id: req.params._id }, 'username',)
    if (!user)
      return res.status(404).send({ error: "Invalid User" })

    const uname = user.username;
    const uid = req.params._id;
    const from = req.query.from ? new Date(moment(req.query.from, 'YYYY-MM-DD').toISOString()) : new Date(0);
    const eod = new moment().endOf('day');
    const now = new moment();
    const to = req.query.to? new Date(moment(req.query.to, 'YYYY-MM-DD').set({'hours' : 23, 'minutes': 59, 'seconds': 59}).toISOString())  : eod.toISOString();
    // req.query.to ? moment.set({ 'hour': 12, 'minute': 0 }).toDate() 
    const limit = (typeof (req.query.limit) != 'undefined') ? Number(req.query.limit) : MAX_QUERY_RECS;
    console.log("from: ", from, "to: ", to);

    const activities = await Activity.find({
      assocId: uid,
      date: { $gte: from, $lte: to }
    }, 'description duration date')
      .limit(limit);

    // This little kludgey bit is here to give me access to 
    // manipulate the return values and order

    factivities = []
    activities.forEach((element) => {
      factivities.unshift({
        description: element.description,
        duration: element.duration,
        date: element.date.toDateString()
      })
    })
    //------------------------------------
    console.log("Returning :", new Object({
      username: uname,
      count: activities.length,
      _id: uid,
      log: factivities

    }));

    res.status(200).send(new Object({
      username: uname,
      count: activities.length,
      _id: uid,
      log: factivities
    }));
  }
  catch (e) {
    res.status(400).send(e);
  }
});

Your project link(s)

solution: https://replit.com/@avatar414/fccbackend4

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:95.0) Gecko/20100101 Firefox/95.0

Challenge: Exercise Tracker

Link to the challenge:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.