Attatched is my jsx file and my delete api (stored as app/api/deleteService/[serviceId]/route.js). In my delete method my service id is always undefined so I am unable to delete the service. Does anyone know how to fix this? Please note my id is in mongodb as _id.
Api:
// /pages/api/deleteService/[serviceId].js
import { connectMongoDB } from "@/lib/mongodb";
import Service from "@/models/services"; // Ensure this model correctly points to your services collection
export async function DELETE(req, res) {
await connectMongoDB();
// Logging the query to debug
console.log("Received query parameters:", req.query);
const serviceId = req.query.serviceId;
if (!serviceId) {
res.status(400).json({ message: 'Service ID is required' });
return;
}
try {
const deleteResult = await Service.deleteOne({ _id: serviceId });
if (deleteResult.deletedCount === 0) {
res.status(404).json({ message: 'No service found with the provided ID' });
return;
}
res.status(200).json({ message: 'Service deleted successfully' });
} catch (error) {
console.error('Error deleting service:', error);
res.status(500).json({ message: 'Failed to delete the service' });
}
}