'NextJs api routes, 2 POST requests how to differentiate
I have a question, I am making an app in NextJS and I have a problem. On one page I can buy, sell and delete the asset. How to differentiate buy and sell if both are POST requests in API Route. What to do, should I add some 'type'? Or Is there a better way?
const handler = async (req: NextApiRequest, res: NextApiResponse) => {
if (req.method === "POST") {
assetIdForBuy = req.body
}
if (req.method === "POST") {
const assetIdForSell = req.body;
}
};
export default handler;
Solution 1:[1]
well, you can simply add some type check like,
if(req.body.data.type === "delete"){ // or something like that whatever fits.
assetIdForDelete = req.body
{
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | okatarismet |
