Hi, I am currently going through the projects on the information security certification. I am currently stuck with a strange error. Right now, I am working on the post request to create a new board. The database is receiving the data in mongodb, but it is still saying I have an error on the localhost.
Here is my api.js file:
'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((err, data) => {
console.log("new board data", data);
if (err || !data) {
console.log(err);
res.send("There was an error saving post.");
} else {
res.json(newThread);
}
});
} else {
boardData.threads.push(newThread);
boardData.save().then((err, data) => {
if (err || !data) {
console.log(err);
res.send("There was an error saving post.");
} else {
res.json(newThread);
}
});
}
});
})
app.route('/api/replies/:board');
}
In case this is necessary, 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;