Anonymous Message board FCC test: Timestamp off by milliseconds, but DID pass on only one occasion

Challenge: https://www.freecodecamp.org/learn/information-security/information-security-projects/anonymous-message-board?messages=success[0]%3Dflash.signin-success

See info below. I’m not sure what I can do to make this test pass, or even if it’s totally within my control. It seems to be the result of some sort of lag. The test DID pass on one occasion, and I thought I had fixed it by moving the line that assigns the timestamp to an earlier place in the code. However, it has not passed since. Please help!

This is the test description and error at the bottom:
You can send a POST request to /api/replies/{board} with form data including text, delete_password, & thread_id. This will update the bumped_on date to the comment’s date. In the thread’s replies array, an object will be saved with at least the properties _id, text, created_on, delete_password, & reported.
// tests completed
// console output
[Error: expected ‘2023-05-29T21:54:02.719Z’ to equal ‘2023-05-29T21:54:03.175Z’]

Here’s my full code: boilerplate-project-messageboard - Replit

Here’s the code for that route:

  app.route('/api/replies/:board')
    .post(async (req, res) => {
      let {thread_id, text, delete_password} = req.body;
      let {board} = req.params;
      let boardDoc = await MessageBoard.findOne({name: board});
      let threadDoc = await boardDoc.threads.id(thread_id);
      if(!boardDoc || !threadDoc) {return res.json({error: 'documents not found'})}
      let timestamp = new Date();
      threadDoc.bumped_on = timestamp; // I believe this is the timestamp in question
      delete_password = bcrypt.hashSync(delete_password, 10);
      let reply = new Reply({
        text: text,
        created_on: new Date(),
        delete_password: delete_password,
        reported: false
      })
      threadDoc.replies.push(reply);
      await boardDoc.save();
      
      return res.redirect(`/b/${board}/${thread_id}/`)
    })

The issue is that you are using TWO different “new Date()”:
The first one:

**let timestamp: new Date();**

The second one:

let reply = new Reply({
        text: text,
        **created_on: new Date(),**
        delete_password: delete_password,
        reported: false
})

SOLUTION:

 app.route('/api/replies/:board')
    .post(async (req, res) => {
     //Declare timestamp at the top
      let timestamp = new Date();

      let {thread_id, text, delete_password} = req.body;
      let {board} = req.params;
      let boardDoc = await MessageBoard.findOne({name: board});
      let threadDoc = await boardDoc.threads.id(thread_id);
      if(!boardDoc || !threadDoc) {return res.json({error: 'documents not found'})}
              // let timestamp = new Date();   DELETE THIS
      threadDoc.bumped_on = timestamp;
      delete_password = bcrypt.hashSync(delete_password, 10);
      let reply = new Reply({
        text: text,
              //created_on: new Date(), DELETE THIS
       //assign timestamp to created_on
        created_on: timestamp,
        delete_password: delete_password,
        reported: false
      })
      threadDoc.replies.push(reply);
      await boardDoc.save();
      
      return res.redirect(`/b/${board}/${thread_id}/`)
    })

Happy coding!!!

1 Like

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