1

How can I pass the variable skip value from front end JS to the Node Serve.JS file? Currently these are the codes that I have. Any idea where I have written wrongly?

Server.js (NodeJs)

app.get("/api/routes", function(req , res){
      // Configure the request
      const skip = req.query.skip;
      var options = {
          url: 'http://dataservice/trainroutes?$skip='+skip,
          method: 'GET',
          encoding: null,
          headers: {
              AccountKey: 'XXX',
              accept: 'application/json'
          }
      }

      request(options, function (error, response, body) {
          if (!error && response.statusCode == 200) {
              res.writeHead(200, response.headers);
              res.write(body);
              res.end();
          }
          else {
              res.send("Error");
          }
      })  
});

Front end JS:

function busseries(){
    var skip = 14000;

    $.ajax({
        url: "/api/routes?skip="+skip,
        success: function(results5) {
            //Set result to a variable for writing
            var objs5 = JSON.stringify(results5);
            var routetimeobjs5 = JSON.parse(objs5);
            console.log(routetimeobjs5)  
        }
    });
}
Fabio
  • 336
  • 3
  • 17
Lawrence
  • 165
  • 8

1 Answers1

1

Pass it with template literals like so.

url: `/api/routes?skip=${skip}`
otejiri
  • 1,017
  • 6
  • 20
  • Currently My skip variable value is '0' instead of the should pose '14000' – Lawrence May 23 '21 at 08:27
  • maybe something else is manipulating your var, change it to `const skip = 14000` and also try hardcoding `/api/routes?skip=14000` to see if it works, then go from there – otejiri May 23 '21 at 08:29
  • What solved it? was it using template literals or using const? – otejiri May 23 '21 at 08:40
  • Your initial suggestion and TusharShahi suggestion have already solved my question initially. There was a conflicting variable value somewhere else in the non related code. Sorry for the confusion... – Lawrence May 23 '21 at 08:47