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:
- when FCC sends a test, it doesn’t store the _id in the body, just params
- 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:
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