The “create and Save a Record of a Model” challenge in the mongodb and mongoose section is not passing for me. When I run the test it shows “test timed out” and fails. Below is my code:
The code below the “fcc code below” comment is how fcc wants me to save the person however mongoose has stopped accepting callbacks for it’s save and other functions. The version in the package.json file accepts callbacks for it’s save function but I had to update to the latest version with npm i mongoose@latest due to a deprecation warning I was getting saying my mongodb atlas url is invalid. Turns out that is was a common problem , check this post: https://www.mongodb.com/community/forums/t/getting-dep0170-deprecationwarning-when-connecting-mongodb-atlas-with-node-lts-slim-node-js-20-9-0/252961
Here is the link to my gitpod project: mongodb and mongoose project
If I try to save the person with callbacks I get an error, if I don’t the tests don’t pass, if I use a lower version of mongoose my app won’t run due to a deprecation warning.
Edit: I’m assuming it’s not passing because I did not use callbacks. I don’t know if I’m correct whatever the reason your help is much appreciated.
Turns out I can run my app even with the deprecation warning so I downgraded my version of mongoose back to the one in the package.json (^5.11.15) and now the tests pass albeit I still get the deprecation warning.
You can modify your code to use async/await throughout:
const createAndSavePerson = async (done) => {
try {
const khal45 = new Person({name: “Khal 45”, age: 20, favoriteFoods: [“rice”, “meat”]});
const result = await khal45.save();
console.log(result);
done(null, result);
} catch (error) {
console.log(error);
done(error);
}
}
It seems like you’re using async/await syntax in your code, which is the modern way of handling asynchronous operations in JavaScript. However, the code provided by the FreeCodeCamp (fcc) test suite is still using the traditional callback-based approach.
You can modify your code to use async/await throughout:
This code will use async/await for both creating the new Person object and saving it to the database. The done callback is called with either the saved Person object or an error if something goes wrong.
Make sure that Person is correctly defined as a Mongoose model before calling createAndSavePerson. Additionally, ensure that done is being called appropriately in the context of your test suite. If the test is still timing out, there might be other issues outside of the provided code snippet.
Thanks for your help but when I run the code I get an error: “Model.findById() no longer accepts a callback”. I decided to just use the version of mongoose the fcc test suites work with and the tests pass for me. I can just use the new version of mongoose with my projects