CodeChimp
CodeChimp

Reputation: 8154

Soft deleting using Mongoose not working

I am trying to soft-delete items using the following Mongoose code:

// Remove a client
exports.delete = (req, res) => {
  logger.info(`Removing Client ${req.params.clientId}`);
  Client.findByIdAndUpdate(
    { _id: req.params.clientId },
    { active: false },
    { new: true }
  )
  .then(client => {
    logger.info('client: ', client);
    if (!client) {
      return res.sendStatus(404);
    }
    res.sendStatus(204);
  })
  .catch(err => {
    logger.error(err);
    res.status(422).send(err.errors);
  });
};

But for some reason the active flag just doesn't want to get set to false. The log shows the clientId is being passed in, and the then code has the Client data, but the active flag still set to true. Do I need to flush or commit somehow?

Edit - Adding schema

export const ClientSchema = new Schema(
  {
    name: {
      type: String,
      trim: true,
      index: true,
      unique: true,
      required: true,
    }
  },
  { collection: 'clients' }
);

Upvotes: 1

Views: 1458

Answers (1)

Orelsanpls
Orelsanpls

Reputation: 23515

As you said in the comment.

You have forgotten to add active field into the mongoose schema.

Upvotes: 1

Related Questions