Naseef Ali
Naseef Ali

Reputation: 69

How to get the parameter from url to a Variable

I am using express. I want to get the json when i request the page with a parameter. i.e.

When I input : http://localhost:3000/GetItemStock?fitemid=1005

I am getting error showing that strItemID is not defined. PLease resolve this issue. Following is the node.js code.

  router.get('/GetItemStock', function(req, res, next) {
      var url = require('url');
      var url_parts = url.parse(req.url, true);
      var param = url_parts.query;
      var strItemID = req.param.fitemid; // $_GET["id"]

      GetItemStock(function(recordset)
      {

      res.send(recordset);
      });
    });

     function GetItemStock(strItemID,callback)
    {
      var sql = require('mssql');
      var config ={
        user: 'sa',
        password: '123456789',
        database: 'ROYAL',
        host: '127.0.0.1,1433'
      };
      var  cn = new sql.Connection(config,function(err)
      {
     
        var str1 = "SELECT tblBranch.fBrName, (sum(tblstocktrans.fqtyin)-sum(tblstocktrans.fqtyout)) from tblStockTrans ,tblbranch where (tblStockTrans.fItemID = '"
        var str2 ="') and (tblBranch.fbrid=tblStockTrans.fbrid) group by fbrname order by fitemid"
        var strQuery = str1 + strItemID + str2
        var request = new sql.Request(cn)
        request.query(strQuery,function(err,recordset/* data*/)
        {
        callback(recordset);
      });
    });
    }
module.exports = router; 

This is the error which I'm getting. Please Help

**c:\api\RmaxErpAPI\routes\index.js:117
    var strQuery = str1 + strItemID + str2
                          ^

ReferenceError: strItemID is not defined**

Upvotes: 3

Views: 72

Answers (2)

Raghbendra Nayak
Raghbendra Nayak

Reputation: 1646

Replace your code

  var url = require('url');
  var url_parts = url.parse(req.url, true);
  var param = url_parts.query;
  var strItemID = req.param.fitemid; // $_GET["id"]

By

  var url = require('url');
  var url_parts = url.parse(req.url, true);
  var query = url_parts.query;
  var strItemID = req.query.fitemid; // $_GET["id"]

Hope it will work.

Upvotes: 1

holydragon
holydragon

Reputation: 6728

Try it like this instead because it is a query string.

var strItemID = req.query.fitemid;

Upvotes: 3

Related Questions