jayendra bhatt
jayendra bhatt

Reputation: 1385

unable to retrieve request param in node js

In my angular js factory, i am making an ajax call to transfer the input model value to the server side ( node js) but when i try to retrieve it , i see 'undefined' .

angular js :

angular.module('name-App').factory('myService', function($http,$rootScope) {

   return {

        getFoos: function(stock) {
            console.log("----------->"+stock.toString());//displays the value correctly over here .
             //return the promise directly.
             return  $http({
        url:'http://localhost:3000/gethistorydata', 
        method: "GET",
        params: stock
     }).then(function(result) {


                            alert("result.data"+result.data);
                            return result.data;
                        }).catch(function(fallback) {
    alert("failed"+fallback + '!!');
  });
        }
   }
});

In node js i have below :

app.get('/gethistorydata',function(req,res){
console.log("--------------->>>>>>");

console.log("stock name = "+req.params.data);
    //res.cookie();
//res.sendFile("F:/customer/public/index.html");
});

req.params.data is undefined . whats wrong with syntax?

Upvotes: 0

Views: 263

Answers (1)

Ryan
Ryan

Reputation: 5973

req.params refers to url path parameters. So you would have to add one to your route for it to be supported. For example a request to /gethistorydata/123 with this route defined(note the added /:id on the end):

app.get('/gethistorydata/:id, function(req, res) {
  console.log('my id is ' + req.params.id');
});

would log "my id is 123".

I think you are looking for req.query. What does your stock object look like? If it looked like this {"price": 123} angular would modify the request to look like /gethistorydata?price=123 and you could change your code to this:

app.get('/gethistorydata, function(req, res) {
  console.log('my stock price is ' + req.query.price');
});

which would log "my stock price is 123"

See the docs for more examples http://expressjs.com/en/api.html#req.query

Upvotes: 1

Related Questions