How do you associate a calculus function within an express function to use in Routes?

Asked

Viewed 82 times

1

To with a very big doubt, I have a function that makes calculations of users' metrics and I want to put it in an express function to be able to return a JSON and use it in a route, It follows the code to use in the routes:

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 have a JSON return.

1 answer

0


Just change the data here:

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

The end result could be just like this:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.