Home > Net >  req.params not giving anything in express nodejs
req.params not giving anything in express nodejs

Time:10-07

I am trying to use the following code to get Id in the url. The problem is that req.params is not giving anything from the route.

app.get("/getAllParcels/:Id",(req,res) => {
        custId = req.params.Id;
        res.send('this is parcel with id:', custId);
});

CodePudding user response:

The Express version of res.send(), only takes one argument so when you do this:

app.get("/getAllParcels/:Id",(req,res) => {
        custId = req.params.Id;
        res.send('this is parcel with id:', custId);
});

it's only going to do this (only paying attention to the first argument):

res.send('this is parcel with id:');

Instead, change your code to this:

app.get("/getAllParcels/:Id",(req,res) => {
    const custId = req.params.Id;
    res.send(`this is parcel with id: ${custId}`);  
});

Note, this properly declares the variable custId using const and it uses a template string (with backquotes) to efficiently incorporate the custId.

CodePudding user response:

First of all, you are missing Identifiers like let and const. Another thing, you have to now send the data with status.

Solution:

    app.get("/getAllParcels/:Id", (req, res) => {
      const custId = req.params.Id;
      res.send(`this is parcel with id: ${custId}`);
    });

  • Related