0

const fireResign = ( Team , job , jobLength , userId, depId, res,next ) => {
  try {
    await Dep.findByIdAndUpdate(
      depId,
      { $pull: {  job : userId }, $inc: { jobLength: -1 } },
      { new: true }
    );

    next();
  } catch (error) {
    return res.status(400).json({
      error: errorHandler.getErrorMessage(error),
    });
  }
}

Job and and jobLength parameters do not work in this function.I realy dont know why but probably its because they could not be property in the function. How could i solve this issue ?

jdee
  • 33
  • 5
  • *"do not work"* is not a clear problem description. You should include the desired behaviour, and the input (the actual call) for which you get something unexpected: what is it, and what was expected instead. Also add what you did to debug the problem. – trincot Nov 10 '21 at 17:23

1 Answers1

2

Use bracket notation:

const fireResign = ( Team , job , jobLength , userId, depId, res,next ) => {
  try {
    await Dep.findByIdAndUpdate(
      depId,
      { $pull: {  [job] : userId }, $inc: { [jobLength]: -1 } },
      { new: true }
    );

    next();
  } catch (error) {
    return res.status(400).json({
      error: errorHandler.getErrorMessage(error),
    });
  }
}
Spectric
  • 30,714
  • 6
  • 20
  • 43
  • 1
    As a word of explanation, up to recently, only literals could be used as keys in object literals. ES6 introduced "Computed Property Names", so any expression (including the function's parameters, like in the OP case) enclosed in [] can be a key, so long as it evaluates to a valid key. – danh Nov 10 '21 at 17:29