API and Microservices - Exercise tracker tests

Hi all,

I’ve bumped into an issue I can’t get my head around.
Namely, when I try to manually test the functionality of my project (by the way, it’s not complete yet), everything works just fine:

  • I’m able to create a new user
  • I’m able to retrieve the list of all users
  • adding exercises with and without date works fine
  • I’m able to get a log of exercises for a specific user.

However, when FCC sends through the tests, all tests related to exercises fail.

I’ve added a few console logs to see what’s going on in the app.post('/api/users/:_id/exercises', async (req, res)=>{...}

/add exercises
app.post('/api/users/:_id/exercises', async (req, res)=>{
  console.log("params: ")
  console.log(req.params)
  console.log("body: ")
  console.log(req.body)
  try{
    const id_check = await User.findOne({"_id": req.body[":_id"]})
    console.log("id_check: " + id_check)
    if(!id_check){
      return res.status(404).send("Invalid Id")
    } 

I see the following logs when I try to add an exercise myself:

params:
{ _id: ‘610821cc1737b420754e1f66’ }
body:
{
‘:_id’: ‘610821cc1737b420754e1f66’,
description: ‘test’,
duration: ‘15’,
date: ‘’
}
id_check: { _id: 610821cc1737b420754e1f66, username: ‘test01’, __v: 0 }

Here’s what happens when FCC sends a test:

params:
{ _id: ‘610826ffe68a3921a894b5f5’ }
body:
{ description: ‘test’, duration: ‘60’, date: ‘1990-01-01’ }
id_check: null

I noted two things:

  1. when FCC sends a test, it doesn’t store the _id in the body, just params
  2. id_check: null would indicate that the user was not created successfully. However, when I check the list of all users, the on in question is clearly there:
    image

I tried to change my code accordingly to my first observation as id_check, look for users in mongo db on the basis of what’s found in req.body:

const id_check = await User.findOne({"_id": req.body[":_id"]})

changed to:

const id_check = await User.findOne({"_id": req.params[":_id"]})

However, id_check still comes back with null

params:
{ _id: ‘610828c12dffe62273181259’ }
body:
{ description: ‘test’, duration: ‘60’, date: ‘1990-01-02’ }
id_check: null

Does anyone have in idea what on earth is happening?

If anyone needs to look at the full code, here’s the invite link:
https://replit.com/join/rmgtqknvob-scratchpads

Any insights are appreciated.
Thanks all

There’s a problem in this line:

const id_check = await User.findOne({"_id": req.params[":_id"]})

The colon isn’t part of the key, it’s used in the router to indicate that this is a dynamic route. But the key must be accessed like this, without colon:

const id_check = await User.findOne({"_id": req.params["_id"]})

// or with dot notation:

const id_check = await User.findOne({"_id": req.params._id})

As for your confusion - fCC doesn’t use your HTML form to send the data, they send an object as request body with values for description, duration and maybe date, but without an _id. It’s better to get that value from req.params.

1 Like

Thanks, this tip helped a lot.
My project is not complete yet but this it a big step forward

The 4th user story is the hardest I think, the rest should be done in no time.

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