我想从MongoDB中删除项.我把所有项目都放到了一页上.然后,我使用map()将物品放到卡片上.一项在卡片中包含编辑BTN和删除BTN,删除BTN具有id(item.id).我抓取了这个id并发送到后端,并通过这个id删除了项目,但我得到了id的未定义.
const deletehandler = async (id) => {
console.log(typeof id, id) // I am getting id = string
try {
await axios.delete('/api/delete', id)
} catch (error) {
console.log(error)
}
}
return ( ...
{
getData.map(item => {
return (
<>
<div key={item.name} className='card'>
<span className='ps-2'>{item.name}</span>
<RiDeleteBin5Line style={{cursor: 'pointer'}}
onClick={() => deletehandler(item._id)}/>
</div>
}
接口/删除文件夹:
import { getSession } from 'next-auth/react';
import db from "@/utils/db";
import User from "@/modules/User";
const handler = async (req, res) => {
console.log(req.body) // ===> here I am getting undefine
await db.connect();
await User.findByIdAndRemove(id)
await db.disconnect();
res.status(200).json({ message: 'success '})
};
export default handler