Home > database >  why I am getting undefined when I try to access on my property in node js
why I am getting undefined when I try to access on my property in node js

Time:03-02

I have sent data from frontend to backend when I console what type of requests I have gotten I can see the data is showing into the console but when I try to access those properties I got undefined. I have also tried with a query, body but both get undefined when I try to access the property.

enter image description here

Backend code:

   // DELETE SHORT URL
        app.delete('/delete/:shortUrl', async (res, req) => {
            console.log(req);
            console.log(req.params, 'req.params');
        })

Frontend:

 // DELETE 
    const deleteUrl = (id) => {
            fetch(`http://localhost:5000/delete/${id}`, {
                method: 'DELETE'
            }).then(res => res.json())
                .then(data => {
                    console.log(data);
                    if (data.deletedCount) {
                        alert('Order Deleted')
                        // const remainingOrders = orders.filter(order => order._id !== id)
                        // setOrders(remainingOrders)
                    }

                })
                .finally(() => setLoadings(false))
    
    }

CodePudding user response:

Based on the Express documentation, the route callback's parameters (req & res) are reversed, so you should have:

app.delete('/delete/:shortUrl', (req, res) => {
  console.log(req);
  console.log(req.params, 'req.params');
})
  • Related