I have defined the following mongoose schema:
const gridSchema = new mongoose.Schema({
rowData: {
type: Array,
},
columnDefs: {
type: Array,
},
defaultColDef: {
filter: { type: Boolean, default: false },
flex: { type: Number, default: 1 },
sortable: { type: Boolean, default: false },
editable: { type: Boolean, default: false },
resizable: { type: Boolean, default: false },
hide: { type: Boolean, default: false },
floatingFilter: { type: Boolean, default: false },
sort: { type: Boolean, default: false },
enableRowGroup: { type: Boolean, default: false },
enablePivot: { type: Boolean, default: false },
enableValue: { type: Boolean, default: false },
rowGroup: { type: Boolean, default: false },
checkboxSelection: { type: Boolean, default: false },
},
user: {
type: mongoose.Schema.Types.ObjectId,
required: true,
ref: 'User',
},
});
I have used many functions in the controller that will help me implement a REST API to Get, Create and Delete single or all records and they work fine. The issue is that the Update function didn't get the job done, after testing it with postman it just returns the same record created the first time and won't update it.
updateGrid function:
const updateGrid = asyncHandler(async (req, res) => {
// Get user using the id in the JWT
const user = await User.findById(req.user.id);
if (!user) {
res.status(401);
throw new Error('User not found');
}
const grid = await Grid.findById(req.params.id);
if (!grid) {
res.status(404);
throw new Error('Grid not found');
}
if (grid.user.toString() !== req.user.id) {
res.status(401);
throw new Errow('Not Authorized');
}
const updatedGrid = await Grid.findByIdAndUpdate(
req.params.id,
req.body,
{
new: true,
}
);
res.status(StatusCodes.OK).json(updatedGrid);
});
What might be wrong? And how if I want to update the defaultColDef only?
Thank you in advance for any help you can provide