Message Board testing bug? Expected timestamp value off by .001 second

Note: I solved this before posting. In case anyone else has the same issue, I moved the line threadDoc.bumped_on = new Date(); up a few lines (as soon as possible without a potential error being thrown). It seems the timestamp was .001 seconds off not due to connection lag, but due to the new date being executed a few lines later.

Original post:
Here’s the project link: https://www.freecodecamp.org/learn/information-security/information-security-projects/anonymous-message-board

I’m getting the error (for You can send a POST request to /api/replies/{board}): [Error: expected ‘2023-05-28T03:45:42.082Z’ to equal ‘2023-05-28T03:45:42.081Z’]

In short, the timestamp is off by .001 seconds. I’m guessing this is not an issue with my code, but is the result of some kind of lag in my connection.

Does anyone have any insight? Interstingly, I don’t get this error the POSTing to api/thread/:board, so I believe the issue comes from the line:
threadDoc.bumped_on = new Date();
You don’t update the bumped date when POSTing to the /api/thread.

  app.route('/api/replies/:board')
    .post(async (req, res) => {
      let {thread_id, text, delete_password} = req.body;
      let {board} = req.params;
      delete_password = bcrypt.hashSync(delete_password, 10);
      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'})}
      threadDoc.bumped_on = new Date(); // if this line comes towards the end, the timestamp will be .001 different from the test's expected
      let reply = new Reply({
        text: text,
        created_on: new Date(),
        delete_password: delete_password,
        reported: false
      })

While I’m at it, regarding the GET routes’ tests, despite not sending the delete_password in the explicit server response, until I set the mongoose schema property to delete_password: {type: String, required: true, select: false}, the GET test would somehow read that property as existing in the response, and fail. The property only existed in my database, so I still have no idea why this was happening.

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