Frontend:
const onDelete = async (linkId) => {
console.log(linkId) // 644aad3188836e6180f5fa05
const res = await fetch("/api/links", {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${localStorage.getItem("token")}`,
},
body: JSON.stringify({ linkId }),
});
if (res.status === 200) {
const updatedLinks = await res.json();
setLinks(updatedLinks);
} else {
alert("Error deleting link");
}
};
Backend
else if (req.method === "DELETE") {
try {
const user = await authMiddleware(req, res);
const { linkId } = req.body;
console.log(linkId) // undefined
if (req.headers["content-type"] !== "application/json") {
res.status(400).json({ msg: "Invalid content type" });
return;
}
const linkIndex = user.links.findIndex((link) => link._id.toString() === linkId);
if (linkIndex !== -1) {
user.links.splice(linkIndex, 1);
await user.save();
res.status(200).json(user.links);
} else {
res.status(404).json({ msg: "Link not found" });
}
} catch (error) {
console.error(error);
res.status(500).json({ msg: "Server error" });
}
}

