Trying to verify whether an API query parameter can be updated. ['age"]
I am comparing it against an array of properties.
Unfortunately, the result keeps returning as false
I have used console.log() to debug.
- parameters
- list of properties
- Result of checking parameters against properties list
router.patch('/users/:id', async (req, res) => {
const updates = Object.keys(req.body);
const properties = ['name', 'age', 'email', 'password'];
console.log(updates)
console.log(properties)
const validateUpdate = updates.every((item) => {
properties.includes(item);
});
console.log(validateUpdate);
try {
if (!validateUpdate) {
console.log(`Property not eligible for update: ${validateUpdate}`);
return res.status(400).send('Property not eligible for update');
}
const updateUser = await User.findByIdAndUpdate(req.params.id, req.body, {
useFindAndModify: false,
new: true,
runValidators: true
});
res.status(200).send("update" + updateUser);
} catch (error) {
res.status(400).send(error)
}
});