The curriculum uses version 5, so use the version of the dependency already in the boilerplate.
Edit: Some more info
Libraries change their APIs, it is just a fact of life. Whenever you see a major version bump major.minor.patch you can expect breaking changes. freeCodeCamp can not possibly update the challenges every time a library updates its API. Whatever version the curriculum teaches is what you should use for the challenges.
Libraries provide you with two options, rewrite your code to the new API using any version migration help they provide, or stay with the old version.
The package.json file uses “mongoose”: “^7.0.4” for the exercise tracker project so I guess is should use the current method? It should pass the tests if the output is correct right? I figure it is probably a good idea to learn the current method anyway.
When these apis change I guess an incredible amount of code must eventually be rewritten.
You can absolutely use version 7 but you will have to use the current API.
I’d say it is as good a time as any to learn about using Mongoose with promises instead of callbacks. It is a cleaner and better way to write the code anyway.
Look at the Mongoose docs to learn more. You can’t just rely on what freeCodeCamp teaches.
Just to be clear. It is a perfectly valid question and I understand it can get confusing. It is just how development goes.
Never update dependencies blindly, always look for breaking changes, and always use the library’s own documentation. If you want to use the latest major version expect breaking changes to old code. Sometimes a little, sometimes a lot.
If you follow tutorials you have to use the version they use or be able to refactor the code on your own for the new version.
It would be great if the backend code taught ditched the callback code and used a more modern approach. On the other hand, as a developer, you should be able to understand both the callback and promise-based approaches and be able to switch between them as needed.
For some reason if there is no record the if (!data) condition is not met. Also if i try to either use a console. log or a res. json with . notation I get undefined
console.log(data) shows up but data.username is undefined. Not really sure why.
Try assigning async to an anonymous function directly after the initial callback function. The reason being is that setting the callback to async seems to not allow the response to carry through.
I figured it out the problem wasnt with the structure of my code it was that i was using. It was the fact that i used model.find instead of model.findOne and model find returns nothing if the record is not found while model find one returns null. So with model.find the if statement would never work either way. Anyway thanks for the suggestions but with model.find nothing was working correctly. It was my first time using async functions so i just assumed that was where the problem was.
Using async for the route handler should work. At least, I have never seen it not work.
Using async/await takes a little getting used to but once you get it, it makes the code much more readable than using callbacks. There are times when you might want to still use .then() as well, it really depends.