Im working on a settings page with 4 input values on the front end.
I have only one table in the db that i want to update correspond to the input value.
Right now im testing this endpoint in postman , lets start with the function itself:
updateController:
<code>exports.updateSettings = async (req, res) => {
// Execute the query and await the result
const data = await Settings.findOne({}, { _id: 1 }).exec();
const settingsId = data._id;
const updatedValue = req.body.newValue;
const fieldToUpdate = req.body.fieldToUpdate;
// Update the settings using the settingsId
await Settings.findByIdAndUpdate(settingsId, {
$set: { [fieldToUpdate]: updatedValue },
console.log("AFTER FINDBYIDANDUPDATE"); // im not getting to this part
res.status(200).json({ message: "update-settings-success" });
console.error("Error updating settings:", err);
res.status(500).json({ message: "failed-update-settings" });
<code>exports.updateSettings = async (req, res) => {
try {
// Execute the query and await the result
const data = await Settings.findOne({}, { _id: 1 }).exec();
const settingsId = data._id;
const updatedValue = req.body.newValue;
const fieldToUpdate = req.body.fieldToUpdate;
// Update the settings using the settingsId
await Settings.findByIdAndUpdate(settingsId, {
$set: { [fieldToUpdate]: updatedValue },
});
console.log("AFTER FINDBYIDANDUPDATE"); // im not getting to this part
res.status(200).json({ message: "update-settings-success" });
} catch (err) {
console.error("Error updating settings:", err);
res.status(500).json({ message: "failed-update-settings" });
}
};
</code>
exports.updateSettings = async (req, res) => {
try {
// Execute the query and await the result
const data = await Settings.findOne({}, { _id: 1 }).exec();
const settingsId = data._id;
const updatedValue = req.body.newValue;
const fieldToUpdate = req.body.fieldToUpdate;
// Update the settings using the settingsId
await Settings.findByIdAndUpdate(settingsId, {
$set: { [fieldToUpdate]: updatedValue },
});
console.log("AFTER FINDBYIDANDUPDATE"); // im not getting to this part
res.status(200).json({ message: "update-settings-success" });
} catch (err) {
console.error("Error updating settings:", err);
res.status(500).json({ message: "failed-update-settings" });
}
};
Settings schema:
<code>const mongoose = require("mongoose");
const settingsSchema = new mongoose.Schema({
const Settings = mongoose.model("Setting", settingsSchema);
module.exports = Settings;
<code>const mongoose = require("mongoose");
const settingsSchema = new mongoose.Schema({
createdAt: {
type: Date,
default: Date.now,
},
minBookingLen: {
type: Number,
required: true,
},
maxBookingLen: {
type: Number,
required: true,
},
maxGuestsPerBooking: {
type: Number,
required: true,
},
breakfastPrice: {
type: Number,
required: true,
},
});
const Settings = mongoose.model("Setting", settingsSchema);
module.exports = Settings;
</code>
const mongoose = require("mongoose");
const settingsSchema = new mongoose.Schema({
createdAt: {
type: Date,
default: Date.now,
},
minBookingLen: {
type: Number,
required: true,
},
maxBookingLen: {
type: Number,
required: true,
},
maxGuestsPerBooking: {
type: Number,
required: true,
},
breakfastPrice: {
type: Number,
required: true,
},
});
const Settings = mongoose.model("Setting", settingsSchema);
module.exports = Settings;
The route:
<code>router.patch("/update-settings", updateSettings.updateSettings);
<code>router.patch("/update-settings", updateSettings.updateSettings);
</code>
router.patch("/update-settings", updateSettings.updateSettings);
Ill just mention that the endpoint are connected and working just the thing is that when i on of the values lets say breakfastPrice and the error message im getting:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
Im expecting to show the update-settings-success message but i guess something is off with how im using the findByIdAndUpdate or maybe nesting both requests is wrong.