How do I fix this error in my POST request: "Error: Thread validation failed: board: Path `board` is required."?

Hi all

When I run the FCC tests on my code, I get an error associated with my POST request. This is the error shown in the console:

Error: Thread validation failed: board: Path `board` is required.
    at ValidationError.inspect (/home/runner/messageboard/node_modules/mongoose/lib/error/validation.js:48:26)
    at internal/per_context/primordials.js:23:32
    at formatValue (internal/util/inspect.js:762:19)
    at inspect (internal/util/inspect.js:328:10)
    at formatWithOptions (internal/util/inspect.js:1989:40)
    at console.value (internal/console/constructor.js:315:14)
    at console.warn (internal/console/constructor.js:348:61)
    at /home/runner/messageboard/routes/api.js:58:28
    at /home/runner/messageboard/node_modules/mongoose/lib/model.js:4931:18
    at processTicksAndRejections (internal/process/task_queues.js:79:11) {
  errors: {
    board: ValidatorError: Path `board` is required.
        at validate (/home/runner/messageboard/node_modules/mongoose/lib/schematype.js:1277:13)
        at /home/runner/messageboard/node_modules/mongoose/lib/schematype.js:1260:7
        at Array.forEach (<anonymous>)
        at SchemaString.SchemaType.doValidate (/home/runner/messageboard/node_modules/mongoose/lib/schematype.js:1210:14)
        at /home/runner/messageboard/node_modules/mongoose/lib/document.js:2690:18
        at processTicksAndRejections (internal/process/task_queues.js:79:11) {
      properties: [Object],
      kind: 'required',
      path: 'board',
      value: undefined,
      reason: undefined,
      [Symbol(mongoose:validatorError)]: true
    }
  },
  _message: 'Thread validation failed'
}

In spite of this error, my post request seems to be working fine and in accordance with FCC’s requirements (my code does exactly as asked). Here is an image of my POST request passing my own functional test:

Although it seems to do as asked, my POST request is unable to pass the FCC test because of the ‘thread validation error’ mentioned earlier (as shown in the following image), hence my asking for your help in this forum post. How can I fix this error?

Here is the POST request code:

  app.route('/api/threads/:board')
    .post(function (req, res){
      //console.log('POST request body: ', req.body);
      //req.body looks like:
      //{ board: 'test board', text: 'wec', delete_password: 'e ' }

      //Create and save new thread document/record in mongoDB
      const createAndSaveThread = function(done) {
        let new_thread = new Thread({
          text: req.body.text,
          delete_password: req.body.delete_password,
          board: req.body.board,
          created_on: new Date(),
          bumped_on: new Date(),
          reported: false,
        })
        new_thread.save(function(err, saved_thread) {
          if(err){
            return console.error(err);
          }else{
            //Return (i.e show) record created and saved to the user
            return res.json(saved_thread);
            done(null,saved_thread);
          };
        });
      };
      createAndSaveThread(() => {});


    })
```

Thanks for your time

Jaime


You are welcome to view and try out my code, found here:
solution: https://replit.com/@jaimeggb/messageboard



**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36</code>

**Challenge:**  Anonymous Message Board

**Link to the challenge:**
https://www.freecodecamp.org/learn/information-security/information-security-projects/anonymous-message-board

Hello!

I suppose it’s because the board from the FCC challenge is on the path instead of the body, hence you should change it like this:

board: req.params.board

Take a look at the docs: Express routing

1 Like

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