Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm using the MEAN stack for a web app. In my controller.js, I have the following:

var refresh3 = function() {
    $http.get('/users').success(function(response) {
        console.log("I got the data I requested");
        $scope.users = response;
        $scope.contact3 = "";
    });
};

refresh3();

This pulls back every object in the "users" collection from my MongoDB. How could I add parameters to this to bring back, for example, only objects where "name" : "Bob" ? I have tried adding parameters in the '/users' parentheses using:

$http.get('/users', {params:{"name":"Bob"}})

I've also tried variants of that, but no luck. Any help would be appreciated!

share|improve this question
    
$http.get('/users', {params:{"name":"Bob"}}) seems correct; please define "no luck" – Manube May 15 '15 at 13:11
    
by default its goes to server as query string unless you specify in $resource, to make actual like /users/bob. in both cases, you still have req.query or req.params accessible in server side code. – YOU May 15 '15 at 13:14
    
"no luck" meaning either the controller breaks and I don't get any data back, or I get the same data I would have expected without any parameters. – Mike Joyce May 15 '15 at 13:21
    
please check whether the data is passed alongside your query (as a query string: you can use developer tools in your browser) and whether it is received by your server; does your mean stack use Mongoose? – Manube May 15 '15 at 13:24
    
Other parts of the site use mongoose, but this part specifically is using MongoJS. – Mike Joyce May 15 '15 at 13:30
up vote 2 down vote accepted

If your server is receiving the data

(and it should, as $http.get('/users', {params:{"name":"Bob"}}) is correct)

On server side, make use of the query string:

req.query

like so:

app.get('/users', function(req,res){
  if(req.query.name){
    db.users.find({"name":req.query.name},function (err, docs) { console.log(docs); res.json(docs); }); 
  }
  else{
    db.users.find(function (err, docs) { console.log(docs); res.json(docs); });
  }
});

WHAT WAS THE ISSUE?

You hinted in your comments that your server was set to respond to the app.get('/users') GET request like so:

db.users.find(function (err, docs) {
 // docs is an array of all the documents in users collection
 console.log(docs); res.json(docs); }); });

So I believe that your angularjs $http get is correct, and your server is receiving the parameters {"name":"Bob"} as it should; it just doesn't know what to do with them: all it is set to do is to return the whole collection in the particular case of a app.get('/users') GET request.


HOW TO CONFIGURE YOUR SERVER FOR REST

You do not have to re-invent the wheel on the server.

Rather, you could consider using a middleware to automate the task (in the present case, the task is to issue a proper MongoDb request when you receive a get query with parameters from the client)

e.g. express-restify-mongoose middleware

share|improve this answer
    
Thanks for your response. Would you be able to clarify where the "if" statement goes in relation to the original app.get? – Mike Joyce May 15 '15 at 18:50
    
Excellent remark: got the statement mangled; will rewrite when access to proper computer in 5 min – Manube May 15 '15 at 19:42
    
ok, I rewrote the "if" statement the way I had originally planned to; but the point is that manually taking care of all possible queries is painful and error prone; it is way easier to rely on a tool especially designed for rest queries, like the one I provided a link to – Manube May 15 '15 at 19:54
    
by the way, I assumed your server was getting the data (ie the request with the {"name":"Bob"} in the query string). Is it the case? – Manube May 15 '15 at 19:56
1  
Hey the new code worked man, I really appreciate it. I will definitely look into the tool you linked to. Really appreciate the help. – Mike Joyce May 15 '15 at 19:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.