QA - Personal Library - Mongoose Operators

POST test for /api/books/:id fails when both $push & $inc are used in route
If I comment out one of the operators in the route the test will run but obviously fail on the commented line data being missing . If both are included the test times out. Can anybody help me understand what’s wrong here please?

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/ Safari/537.36

Challenge: Quality Assurance Projects - Personal Library

Link to the challenge:

I’m seeing different issues.

res.text is not an express method are you not getting an error? Use res.send (there is a middleware express.text but that is something different).

The way you are using the find Mongoose method is not working as you expect. I’m pretty sure it will set err to null at times and then you are checking if it is a truthy value. Remove the .then and just use the await. Then check what is returned.

let doc = await someModel.findById(someId);

if (!doc) return res.send('Not found');

if (doc) return res.json({ data: 'some data'});

When I make that change it passes all but the last delete tests which aren’t implemented yet.

Thanks lasjorg, I really appreciate your help.

I’ve had a go at implementing your suggestions and now I’m more confused.
None of the tests are timing out now but my ‘Test POST /api/books/[id] with comment’ test is showing as failing but… when I check the error code it’s actually an issue in my GET test on line 132.

  1. Do you know why I’ve got an assertion error flagging on a test that’s showing as passed? And no error for the ‘failed’ test?
  2. It seems on my ‘GET /api/books/[id]’ my res.body is returning an empty {} when an invalid id is used. It should be set to return res.send(‘no book exists’) so I’m not sure why?

I’ve setup a new replit: boilerplate-project-library (1) - Replit
GitHub - Gerald-Simpson/boilerplate-project-library

Okay so I worked out why the POST test was showing as failing - I had a done as a parameter on an async function. However, I still don’t understand why the GET test is passing but spitting out an assertion error…

Annnd I’ve also finally found the GET issue. For some reason my id to fail was too long? Now that it’s shorter it works…

Are you passing all the tests now? I can’t load the live Replit only the editor.

Not sure I understand this. What was it before and what is it now?

Ahh my bad didn’t realise. Yeah they’re passing now - just working through the remaining POST & DELETE’s now.
So when I changed from:
or anything shorter the res.body would work properly and not return an empty object. Not quite sure why.

Hmmmmmm I seem to be running into a similar issue on my delete route and I can’t work out why…

Where are you working on this? I don’t see any code in the Replit for the .delete that would handle it.

Images are hard to use, please add your code to the Replit.

If you add a log after the two ifs do you hit it?

Apologies, I’m working on it locally in neovim, then uploading to git & importing to replit to pass the tests / for your review. I’ve uploaded a new replit. Is there a better way for me to do this for you that would allow you to run my code?
I’ve done some more testing - a log after the two ifs isn’t hit.
I think the error has something to do with the async / await not working or waiting before responding. As if I comment the await line res.body no longer returns {}.
However, confusingly if I only have res.send(‘test’) in my route the test passes which makes no sense.

& error in-case you can’t run the replit:

I assume you are talking about the test callback function?

Try removing async, pass done to the test callback and and call it. I would also call it inside the end callback after the asserts.


No sorry I’m reffering to the async / await & res.send in the DELETE route.
Also, I can’t remove the async from the test as I need an await for the .save() to add an entry to the database.

You should be able to use .then() as well on save if needed.

Instead of doing what you are in the delete test, you can use your API and the POST test you have before the DELETE test. So you save the id you get back from the POST test to a top-level variable and reuse that for the other tests that need a book (e.g. the delete test).

Ok that makes sense but can you help me understand what’s wrong with my DELETE route please?

It seems that if I return an object or an array in a res.send() it works but if I return a string it returns an empty object?

Log out res, I know it is a big structure but if you look for the body property right by it, it should have another property with your text data. Use that property.

Thank you. Sometimes I can’t see the wood for the trees. That was the hint I needed.

Sorry to be back again so soon. But I think I’ve done everything correctly now and all my functional tests are passing but all the freecodecamp tests are failing. Do you have any idea why?
The replit seems to be reloading ‘/’ at the end of the tests so they’re not timing out.
My secrets for the DB & test_env are setup correctly.

I might suggest you try it on Glitch.

Replit seems to be having issues. I have to use the -- version of your URL (to avoid SSL errors) but even if I fork it and use the normal URL I get CORS blocked on one of the POSTs. It’s like it never sets the correct header.