This document outlines the issues encountered and fixes applied to resolve problems in the Web Debugging Challenge 2 project.
Problem: There was a typo in the route handler for the '/sighting' endpoint.
Fix: Changed:
app.get('/sighting', SightingCtrl.raed);
To:
app.get('/sighting', SightingCtrl.read);
Problem: The project was using an outdated version of Mongoose, leading to compatibility issues.
Fix: Updated Mongoose to the latest version:
npm install mongoose@latest
Problem: After updating Mongoose, an error occurred due to the use of callbacks in query execution:
C:\Users\DELL 7510\Omnibus\Test2\web-debugging-challenges\challenge2\node_modules\mongoose\lib\query.js:4353
throw new MongooseError('Query.prototype.exec() no longer accepts a callback');
Fix: Updated controller methods to use async/await instead of callbacks. For example:
// Before
exports.read = function(req, res) {
Sighting.find({}, function(err, sightings) {
if (err) return res.status(500).send(err);
return res.send(sightings);
});
};
// After
exports.read = async function(req, res) {
try {
const sightings = await Sighting.find({});
return res.send(sightings);
} catch (err) {
return res.status(500).send(err);
}
};
Problem:
After updating Mongoose, findByIdAndRemove
is deprecated.
Fix:
Updated the method to use findByIdAndDelete
. For example:
// Before
delete: async function(req, res) {
try {
const result = await User.findByIdAndRemove(req.params.id);
if (!result) {
return res.status(404).send({ message: 'User not found' });
}
res.send(result);
} catch (err) {
res.status(500).send({ message: 'Internal Server Error', error: err.message });
}
}
// After
delete: async function(req, res) {
try {
const result = await User.findByIdAndDelete(req.params.id);
if (!result) {
return res.status(404).send({ message: 'User not found' });
}
res.send(result);
} catch (err) {
res.status(500).send({ message: 'Internal Server Error', error: err.message });
}
}
-
Typo Fix: The typo in the route handler was a simple spelling error. Correcting 'raed' to 'read' ensures that the correct controller method is called when the '/sighting' endpoint is accessed.
-
Mongoose Update: Updating to the latest version of Mongoose is generally a good practice to ensure compatibility with the latest Node.js versions and to benefit from the latest features and bug fixes.
-
Async/Await Implementation: The error message indicated that Mongoose no longer supports callbacks in query execution. Switching to async/await syntax is the recommended approach for handling asynchronous operations in modern JavaScript. This change makes the code more readable and easier to maintain while also resolving the compatibility issue with the latest Mongoose version.
-
Deprecated Method Update: Replacing
findByIdAndRemove
withfindByIdAndDelete
ensures compatibility with the latest Mongoose version and follows best practices for database operations.
To ensure the functionality of our API endpoints after applying the fixes, API testing was conducted using Postman. All API tests passed successfully. The endpoints correctly return expected responses, and proper error handling is in place for invalid requests.