Exercise Tracker Test Case related Logs Failing

Tell us what’s happening:
Failing all the GET request to /api/users/:_id/logs
i.e
test 5 expects a GET request to /api/users/:_id/logs will return a JSON object
that contains a ‘log’ property which is an array of objects that contain a date (date.toDateString format), a description (String), and a duration (Number).

Test 6 expects that that same route ( GET request to /api/users/:_id/logs ) return object will include a count (Number) property which correlates with the length of the log property.

Code

const exerciseSchema = new mongoose.Schema({
description: String,
duration: Number,
date: Date
})
const userSchema = new mongoose.Schema({
username: String,
count: Number,
log: [exerciseSchema]
})
const user = mongoose.model('User', userSchema)

app.get('/api/users/:_id/logs', (req, res) => {
	const userId = req.params._id
	const from = req.query.from
	const to = req.query.to
	const limit = req.query.limit

	user.find({ _id: userId }, { 'log._id': 0 }, (err, data) => {
		if (err) res.json(err)
		if (data.length != 0) {
			const tempLog = []
			data[0].log.forEach(element => {
				const temp = {
					description: element.description,
					duration: parseInt(element.duration),
					date: element.date.toDateString()
				}
				if (from && to) {
					if (element.date >= new Date(from) && element.date <= new Date(to)) {
						tempLog.push(temp)
					}
				} else if (from) {
					if (element.date >= new Date(from)) {
						tempLog.push(temp)
					}
				} else if (to) {
					if (element.date <= new Date(to)) {
						tempLog.push(temp)
					}
				} else {
					tempLog.push(temp)
				}
			});
			const logs = {
				_id: data[0]._id,
				username: data[0].username,
				count: parseInt(data[0].count),
				log: limit ? tempLog.slice(0, limit) : tempLog
			}
			res.json(logs)
		} else {
			res.send('Unknown userId')
		}
	})
})

Sample Output

{"_id":"61efd8b87bc803b8d83c3285",
"username":"roronoaslefteye",
"count":4,
"log":[{"description":"push ups","duration":30,"date":"Wed Feb 02 2022"},{"description":"pull ups","duration":40,"date":"Wed Feb 02 2022"},{"description":"abs","duration":20,"date":"Tue Jan 25 2022"},{"description":"legs","duration":60,"date":"Fri Jan 28 2022"}]}

Your project link(s)

solution: https://replit.com/@hardik98/exercise-tracker

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.99 Safari/537.36

Challenge: Exercise Tracker

Link to the challenge:

I’d say there is some mismatch between description and tests: freeCodeCamp/exercise-tracker.md at d964e18339d84b08e04a23c469b2da4028a4675a · freeCodeCamp/freeCodeCamp · GitHub

Tests expect an array of flat objects while description says that logs should be nested inside the user object :thinking:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.