Exercise Tracker Project, how to list result between dates and how to set result limit

Exercise Tracker Project, how to list result between dates and how to set result limit
0

#1

Hello.

I am trying to list results between dates. https://learn.freecodecamp.org/apis-and-microservices/apis-and-microservices-projects/exercise-tracker

And here is my code:

// show exercises of user
router.get('/api/exercise/log', function(req, res, next) {
		let userId = req.query.userId;
		let from = new Date(req.query.from);
		let to = new Date(req.query.to);
		let limit = req.query.limit
	User.findOne({ userId: userId}, function(err, user) {
		if (err) {
			next(err);
			return;
		} if (!user) {
			res.json({ error: 'user not found!'});
		} else {
			let results = user.exercises
			res.json({
				exercises: results
			});
		}
	})
});

GET users’s exercise log: GET /api/exercise/log?userId=userid&from=fromdate&to=todate&limit=howmanyitem


#2

I see you have the needed querystring parameter values, but you have not written any logic which uses them. Can you share with us what logic you are contemplating to use which incorporates the variables from, to, and limit? We are not just going to give you the code. You need to show you have tried some type of logic and then we can guide you to a complete solution.


#3

I spent 2 days for that. But I could not figure out any way.

Because of that I did not use the parameters. I dont know what to do.


#4

I solved the problem. Thank you.

// show exercises of user
router.get('/api/exercise/log', function(req, res, next) {
		let userId = req.query.userId;
		let from = new Date(req.query.from);
		let to = new Date(req.query.to);
		let limit = req.query.limit;
	User.findOne({ userId: userId}, function(err, user) {
		if (err) {
			next(err);
			return;
		} if (!user) {
			res.json({ error: 'user not found!'});
		} else {
			let results = user.exercises
			if(to && from) {
				results = results.filter(function(item) {
					return item.date >= from && item.date <= to
				});
			}
			if(!isNaN(limit)) {
				results = results.slice(0, limit);
			}
			res.json({
				exercises: results
			});
		}
	})
});