How do you associate a calculation function inside an express function to use in routes?

1

With a very big question, I have a function that does calculations of user metrics and I want to put it in an express function to be able to return a JSON and use it in a route, follow the code to use in the routes: / p>

const calculate_followers_week = (req, res) => {
  let user_id = req.params.id;
  UserHistorySchema.find(user_id ? {
      _id: mongoose.Types.ObjectId(user_id)
    } : {}, {
      'history.created_at': 1,
      'history.meta.indicators': 1
    })
    .lean()
    .exec()
    .then(data => {
      res.status(200).json(data);
    })
    .catch(err => {
      res.status(400).json(err);
    })
}

I want to put a calculation function inside it to get a JSON return.

    
asked by anonymous 16.05.2017 / 00:56

1 answer

0

Just change the data here:

.then(data => {
    res.status(200).json(data);
})

The end result could be as well:

.then(data => {
    var processado = minhaFuncao(data);
    res.status(200).json(processado);
})
    
16.05.2017 / 03:36