Kabilesh
Kabilesh

Reputation: 1012

angular http get with node js

I am querying data from mongodb and sending that as a json to a URL

querying data from mongodb

router.get('fillSurvey/:ID', function (req, res) {
    Survey.findOne({_id:req.params.ID}, function ( err, survey, count ){
     res.send(json);
   }); 
});

I want to use angular http get to fetch this json data. The problem I have is what should be the url in angular http get? The problem arises because there is a /:ID in the above url.

Upvotes: 1

Views: 846

Answers (2)

Gangadhar Jannu
Gangadhar Jannu

Reputation: 4414

That will not cause any issue in angularJS. You can simply access fillSurvey/id URL.

Try the below code

var data=10; // ID that you want to pass
var requestParam={
  method:"GET",
  params:data,
  url:"fillSurvey/"+data // URL 
}
$http(requestParam).then(function(success){
  console.log(success);
}, function(error){
  console.log(error);
});

Upvotes: 1

Shekhar Tyagi
Shekhar Tyagi

Reputation: 1674

This is the code of your nodejs.

router.get('/fillSurvey/:ID', function (req, res) {
    var ID = req.params.ID;
    Survey.findOne({_id: ID}, function (err, survey) {
        if (err) {
            res.json({status: 0, message: err});
        } else if (!survey) {
            res.json({status: 0, msg: "not found"});
        } else {
            res.json({status: 1, message: survey});
        }
    })
});

Now if you want to call this api use the url let us assume your id is '1234'

"http://localhost:portno/fillSurvey/1234"

Upvotes: 2

Related Questions