https://replit.com/@Geoelycom/boilerplate-project-exercisetracker#server.js

Tell us what’s happening:

Your project link(s)

solution: https://boilerplate-project-exercisetracker.geoelycom.repl.co

I keep failing one of the tests when I try submitting the microservices project, been stuck on it, I don’t know where I’m getting it. I keep getting this failed test
" A request to a user’s log ( /api/exercise/log ) returns an object with a count property representing the number of exercises returned ". If you can see, I am capturing the count property and using the .lenght to keep track of the data, hence it should work, but for one reason I don’t seem to find, the code is not passing, please I need help figuring out where I’m missing it.
my code…

 const express = require('express')
const app = express()
const cors = require('cors')
const shortId = require('shortid')
const mongoose = require('mongoose')
const bodyParser = require('body-parser')
require('dotenv').config()

const { schema } = mongoose;
app.use(cors())
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static('public'))
app.get('/', (req, res) => {
    res.sendFile(__dirname + '/views/index.html')
});

//connection to the database
mongoose.connect(process.env.DB_URI, { useNewUrlParser: true, useUnifiedTopology: true });
console.log(mongoose.connection.readyState);
// creating a Mongoose Schema
const personSchema = new mongoose.Schema({ username: { type: String, unique: true } });
const Person = mongoose.model("Person", personSchema);

const exerciseschema = new mongoose.Schema({ userId: String, description: String, duration: Number, date: Date });
const Exercise = mongoose.model("Exercise", exerciseschema)

app.post("/api/exercise/new-user", (req, res) => {
    // creating a new user in the db
    const newPerson = new Person({ username: req.body.username });
    // save newly created user in the database.
    newPerson.save((err, data) => {
        if (err) {
            res.json("username already taken")
        } else {
            res.json({
                "username": data.username,
                "_id": data.id
            })
        }

    })
})

app.post("/api/exercise/add", (req, res) => {
    let { userId, description, duration, date } = req.body;
    if (!date) {
        date = new Date();
    }
    Person.findById(userId, (err, data) => {
        if (!data) {
            res.send("Unknown userId")
        } else {
            const username = data.username;
            let newExercise = new Exercise({ userId, duration, description, date })
            newExercise.save((err, data) => {
                res.json({
                    _id: userId,
                    username,
                    description,
                    duration: + duration,
                    date: new Date(date).toDateString()
                })

            })
        }


    })
})

app.get("/api/exercise/log", (req, res) => {
    const { userId, from, to, limit } = req.query;
    Person.findById(userId, (err, data) => {
        if (!data) {
            res.send("Unknown userId")
        } else {
            const username = data.username;
            console.log({ "from": from, "to": to, "limit": limit })
            Exercise.find({ userId }, {
                    date: {
                        $gte: new Date(from),
                        $lte: new Date(to)
                    }
                }).select(["id", "description", "duration", "date"]).limit(+limit)
                .exec((err, data) => {
                    let customdata = data.map(exer => {
                        let dateFormatted = new Date(exer.date).toDateString();
                        return { id: exer.id, description: exer.description, duration: exer.duration, date: dateFormatted}
                    })
                    if (!data) {
                        res.json({
                          "count": 0,
                            "userId": userId,
                            "username": username,
                            "log": []
                        })

                    } else {
                        res.json({
                          "count":data.lenght,
                          "username": username,
                          "userId": userId,
                            "log": customdata
                        })
                    }
                    
                })
        }
    })
})

app.get("/api/exercise/users", (req, res) => {
        Person.find({}, (err, data) => {
            if (!data) {
                res.send("No users")
            } else {
                res.json(data)
            }
        })
    })
    // if we don't find a middleware
app.use((req, res, next) => {
        return next({ status: 404, message: "not found" })
    })
    // Error to handle Middleware if we encounter an error



const listener = app.listen(process.env.PORT || 3000, () => {
    console.log('Your app is listening on port ' + listener.address().port)
})

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36.

Challenge: Exercise Tracker

Link to the challenge:

Hello there,

This is what your app returns:

{"username":"test_shaun","userId":"60732c76a6a3ff003a7572ea","log":[{"id":"60732c87a6a3ff003a7572eb","description":"Some description","duration":123,"date":"Sun Apr 11 2021"}]}

This is what the test says:

A request to a user’s log ( /api/exercise/log ) returns an object with a count property representing the number of exercises returned.

Have a look for typos:

Can you see the problem?

Hope this helps

No sorry I can’t see it. Maybe you can be a little more open. I know its not capturing the count property, I just don’t know why :hot_face::hot_face:

No worries. Sometimes we devs spend too much time in front of the screen, and miss the smallest stuff.

How about now…read it slowly…

EDIT: I do not mean to be patronizing; it is just a much better feeling realising your own mistakes than someone else pointing it out, but here it is:

The property length is misspelt

Shoot me dead please😭. I just saw it.

1 Like

Nice job!

I would have expected the console to throw an informative error, though?

Trust me I know. I feel so stupid right now. I have been on this shit for the past 24hours.

Using Repl so I didn’t even take notice to console.log that​:pleading_face::pleading_face:.
Thank you mate.

1 Like

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