Require the express-async-handler package in the quoteController.js
file
const asyncHandler = require("express-async-handler");
Wrap the controller functions with async
const asyncHandler = require("express-async-handler");
const getQuotes = async (req, res) => {
res.status(200).json({ message: "Get quotes" });
};
const createQuote = async (req, res) => {
if (!req.body.text) {
res.status(400);
throw new Error("Please add a text field");
}
res.status(200).json({ message: "Create quote" });
};
const updateQuote = async (req, res) => {
if (!goal) {
res.status(400);
throw new Error("Goal not found");
}
res.status(200).json({ message: `Update quote, ${req.params.id}` });
};
const deleteQuote = async (req, res) => {
if (!goal) {
res.status(400);
throw new Error("Goal not found");
}
res.status(200).json({ message: `Delete quote, ${req.params.id}` });
};
module.exports = {
getQuotes,
createQuote,
updateQuote,
deleteQuote,
};
Wrap the controller functions with asyncHandler
const asyncHandler = require("express-async-handler");
const getQuotes = asyncHandler(async (req, res) => {
res.status(200).json({ message: "Get quotes" });
});
const createQuote = asyncHandler(async (req, res) => {
if (!req.body.text) {
res.status(400);
throw new Error("Please add a text field");
}
res.status(200).json({ message: "Create quote" });
});
const updateQuote = asyncHandler(async (req, res) => {
if (!goal) {
res.status(400);
throw new Error("Goal not found");
}
res.status(200).json({ message: `Update quote, ${req.params.id}` });
});
const deleteQuote = asyncHandler(async (req, res) => {
if (!goal) {
res.status(400);
throw new Error("Goal not found");
}
res.status(200).json({ message: `Delete quote, ${req.params.id}` });
});
module.exports = {
getQuotes,
createQuote,
updateQuote,
deleteQuote,
};