I am working on configuring a patch request for the endpoint "/api/user?username=idHere". This patch request should accept a JSON body and update the user in MongoDB with the new key-value pairs. Currently, the line "{$set: {key: req.body[key]}}" is being interpreted literally, trying to set the word "key" as the key instead of the actual key from the request body. How can I achieve this goal correctly? Below is my current code attempting this.
const updateUser = (req, res) => {
const db = mongoConnection.getDb();
const keys = Object.keys(req.body);
for (key in keys) {
db.collection('users').updateOne(
{username: req.query.username},
{$set: {key: req.body[key]}}
)
}
}