Exercise Tracker project : unable to pass 4th test

Project: https://boilerplate-project-exercisetracker.berkesenturk.repl.co
Hi I’ve wrote the code as follows:


let userSchema = new mongoose.Schema({
  username: {type: String, required: true, unique: true}

})
let exerciseSchema = new mongoose.Schema({
  userid: {type: String},
  description: {type: String},
  duration: {type: Number},
  date: {type: Date}
})

let User = mongoose.model('User', userSchema)
let Exercise = mongoose.model('Exercise', exerciseSchema)

app.post('/api/users/', bodyParser.urlencoded({ extended: false }),(req, res) => {
  console.log(req.body)
    
  var user = new User({username: req.body['username']});

  user.save()
      .then(item => {
        res(201).json({"username": item.username, "_id": item._id});
      })
      .catch(err => {
        res.status(400).send("unable to save to database");
      });
  })

app.get('/api/users', (req, res) => {
  User.find({}, function(err, users) {
    var userMap = [];

    users.forEach(function(user) {
      userMap.push(user);
    });

    res.send(userMap);  
  });
});

app.post('/api/users/:id/exercises', 
         bodyParser.urlencoded({ extended: false }),
         (req, res) => {
  
  User.findById(req.body[':_id'], (err,data) => {
    if(!data) {
      res.send("Unknown user id")
    
    } else {

    let newExercise = new Exercise({
      userid: req.body[':_id'],
      description: req.body.description,
      duration: req.body.duration,
      date: req.body.date
    })
    
    newExercise.save()
              .then(item => {
                  res.json({
                            "userid": item.userid, 
                            "username": data.username,
                            "description": item.description,
                            "duration": item.duration,
                            "date": item.date = (!req.body.date) ? new Date().toDateString() : 
                                                                  new Date(req.body.date).toDateString()
                            });
                })
              .catch(err => {
                  res.status(400).send("unable to save to database");
                });
    }
  })

  })

Even though the output is correct, still test doesn’t pass. Can you see the problem what it is?

It would help to have a link to your project/code, but a common pitfall in this challenge is that duration needs to be of type number. Looks like in your response, it’s a string.

It’s : https://boilerplate-project-exercisetracker.berkesenturk.repl.co

Thanks!

No the type of duration is number

You have defined type number in the Schema, but you’re still storing it as string, and Mongo/Mongoose won’t complain about that, it won’t convert the string to a number for you, you have to do that yourself. You could, for example, say in your Schema that duration is of type Array. If you then try to store a string instead, it would work perfectly.

I tried to test your app but there seem to be more issues, I couldn’t create a user and save it to the database. But the response should look like this:

fcc

Note how duration is a number, while the other values are a string.

Take a close look at what this is responding with.

Hope this helps

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