I am making a trello like app with mern stack. Everything going smooth until I create a column. Postman says “Invalid board ID”. When I hardcopy an existing boardID to the route the column is created
columnController
const createColumn = async (req, res) => {
const { title } = req.body;
const boardId = req.params.boardId;
const userId = req.user.userId;
console.log('Received boardId:', boardId); // Debugging line
console.log('Is boardId valid:', mongoose.Types.ObjectId.isValid(boardId));
if (!req.user) {
return res.status(400).json({ message: 'User not authenticated' });
}
if (!mongoose.Types.ObjectId.isValid(boardId)) {
return res.status(400).json({ message: 'Invalid board ID' });
}
try {
const newColumn = new Column({ title, user: userId, board: boardId });
const column = await newColumn.save();
res.status(201).json(column);
} catch (err) {
console.log(err);
res.status(500).json({ message: 'Server error' });
}
};
column model
const columnSchema = new mongoose.Schema({
title: { type: String, required: true },
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
board: { type: mongoose.Schema.Types.ObjectId, ref: 'Board', required: true },
});
board model
const boardSchema = new mongoose.Schema({
title: { type: String, required: true },
description: { type: String },
user: { type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
createdAt: { type: Date, default: Date.now },
});```
New contributor
Panagiotis Kormpis is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.