Mongoose findOneAndUpdate is not accepting switch case values

Viewed 547

I was trying to create a single function to update different types of options in Mongoose model and encountered this strange behavior. Here's what I was trying to do.

module.exports.updateUser = function(id, action, status, callback) {
const query = {
    _id: id
};

let field = '';
switch (action) {
    case 'download':
        field = 'download_permission';
        break;
    case 'upload':
        field = 'upload_permission';
        break;
    case 'view':
        field = 'view_permission';
        break;
    case 'edit':
        field = 'edit_permission';
        break;

}
User.findOneAndUpdate(query, {
    $set: {
        field: status,
        last_updated: moment().format('llll')
    }
}, callback);

};

Now, if I try something like :

User.findOneAndUpdate(query, {
$set: {
    'edit_permission': status,
    last_updated: moment().format('llll')
}
}, callback);

};

It actually updates the document in the mongodb. Can someone explain me why second works and not the first (doesn't update the document). Thanks.

3 Answers
Related