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.