Frustrating issue with Anonymous message board project

Hi, I am working on the Information security projects and am stuck on the anonymous message board project. Currently working on the post request to create replies, but I am running into a strange error. Here is my code for the route:

'use strict';
const ThreadModel = require("../models").Thread;
const ReplyModel = require("../models").Reply;
const BoardModel = require("../models").Board;

module.exports = function (app) {
  
  app.route('/api/threads/:board').post((req, res) => {
    const { text, delete_password } = req.body;
    let board = req.body.board;
    if (!board) {
      board = req.params.board;
    }
    console.log("post", req.body);
    const newThread = new ThreadModel({
      text: text,
      delete_password: delete_password,
      replies: []
    });
    console.log("new thread", newThread);
    BoardModel.findOne({ name: board }).then(boardData => {
      if (!boardData) {
        const newBoard = new BoardModel({
          name: board,
          threads: []
        });
        console.log("new board", newBoard);
        newBoard.threads.push(newThread);
        newBoard.save().then(data => {
          console.log("new board data", data);
          res.json(newThread);
        }).catch(err => {
          console.log(err);
          res.send("There was an error saving post.");
        });
      } else {
        boardData.threads.push(newThread);
        boardData.save().then(data => {
          console.log("new board data", data);
          res.json(newThread);
        }).catch(err => {
          console.log(err);
          res.send("There was an error saving post.");
        });
      }
    }).catch(() => res.send("Could not create board."));
  }).get((req, res) => {
    const board = req.params.board;
    BoardModel.findOne({ name: board }).then(data => {
      console.log("data", data);
        const threadsObj = data.threads.map(thread => {
          const {
            _id,
            text,
            created_on,
            bumped_on,
            reported,
            delete_password,
            replies
          } = thread;
          return {
            _id,
            text,
            created_on,
            bumped_on,
            reported,
            delete_password,
            replies,
            replycount: replies.length
          };
        });
        res.json(threadsObj);
    }).catch(() => res.json({ error: "No board with this name." }));
  }).put((req, res) => {
    console.log("put", req.body);
    const { report_id } = req.body;
    const board = req.params.board;
    BoardModel.findOne({ name: board }).then(boardData => {
      const date = new Date();
      let reportedThread = boardData.threads.id(report_id);
      reportedThread.reported = true;
      reportedThread.bumped_on = date;
      boardData.save()
      .then(() => res.send("Update successful"))
      .catch(() => res.json({error: "Could not update"}));
    }).catch(() => res.json({ error: "Board not found"}));
  }).delete((req, res) => {
    console.log("delete", req.body);
    const { thread_id, delete_password } = req.body;
    const board = req.params.board;
    BoardModel.findOne({ name: board }).then(boardData => {
      if (boardData.threads.delete_password === delete_password) {
        boardData.threads.deleteOne(thread_id);
      } else {
        res.send("Incorrect Password.")
        return;
      }
      boardData.save()
      .then(() => res.send("Delete successful"))
      .catch(() => res.json({ error: "Could not delete."}));
    }).catch(() => res.json({ error: "Board not found"}));
  });
  app.route('/api/replies/:board').post((req, res) => {
    console.log("thread", req.body);
    const { thread_id, text, delete_password } = req.body;
    const board = req.params.board;
    const newReply = new ReplyModel({
      text: text,
      delete_password: delete_password
    });
    BoardModel.find({ name: board }).then(boardData => {
      const date = new Date();
      let repliedThread = boardData.threads.id(thread_id)
      repliedThread.bumped_on = date;
      repliedThread.replies.push(newReply);
      boardData.save()
      .then(updatedData => res.json(updatedData))
      .catch(() => res.json({ error: "Could not add reply."}));
    })
  })
}

Here is the error that I am getting:
Screen Shot 2024-06-17 at 10.37.49 AM

I am missing a .catch() at the end, but I did that so you can see the error.

I am unable to see your BoardModel class. However my assumption is that for whatever reason threads may be undefined at that point in time.

Hi, sorry about that. Here is my models.js file:

const mongoose = require('mongoose');
const { Schema } = mongoose;

const date = new Date();

const replySchema = new Schema({
    text: { type: String },
    delete_password: { type: String },
    reported: { type: Boolean, default: false },
    created_on: { type: Date, default: date },
    bumped_on: { type: Date, default: date },
    });
const threadSchema = new Schema({
    text: { type: String },
    delete_password: { type: String },
    reported: { type: Boolean, default: false },
    created_on: { type: Date, default: date },
    bumped_on: { type: Date, default: date },
    replies: { type: [replySchema]}
});
const boardSchema = new Schema({
    name: { type: String },
    threads: { type: [threadSchema] }
});

const Thread = mongoose.model("Thread", threadSchema);
const Reply = mongoose.model("Reply", replySchema);
const Board = mongoose.model("Board", boardSchema);

exports.Thread = Thread;
exports.Reply = Reply;
exports.Board = Board;

Please post a repo with your code.


let reportedThread = boardData.threads.id(report_id);

Are you sure the id property is a method?

If it’s just an id property, are you sure it isn’t _id?

I would suggest you log out boardData

Hi, here is the repo with the rest of the code: GitHub - jamesnascimento1994/boilerplate-project-messageboard: A boilerplate for a freeCodeCamp project.

And yes, that id property is a method. It works on the other routes. It’s just not working on the reply post request, which is the last route so far in my api.js file at let repliedThread = boardData.threads.id(thread_id)

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