1

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.

5
  • var url = 'fillSurvey/' + 12 if you wanna get with id = 12 Commented Feb 4, 2017 at 5:09
  • is there a way to access all IDs that are passed in the url? a common way for all IDs...Because I get the ID in the above code from <a href="SubmitSurvey/<%= survey._id %> . Commented Feb 4, 2017 at 5:17
  • just create new router for all survey Commented Feb 4, 2017 at 5:19
  • Yes I use express js Commented Feb 4, 2017 at 5:20
  • /:ID is way to represent the URL contains an id. So you can pass directly 'fillSurvey/id` in angular, Where id should be any id from your application Commented Feb 4, 2017 at 5:22

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

Comments

1

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);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.