3

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!

10
  • $http.get('/users', {params:{"name":"Bob"}}) seems correct; please define "no luck" Commented May 15, 2015 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. Commented May 15, 2015 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. Commented May 15, 2015 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? Commented May 15, 2015 at 13:24
  • Other parts of the site use mongoose, but this part specifically is using MongoJS. Commented May 15, 2015 at 13:30

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your response. Would you be able to clarify where the "if" statement goes in relation to the original app.get?
Excellent remark: got the statement mangled; will rewrite when access to proper computer in 5 min
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
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?
Hey the new code worked man, I really appreciate it. I will definitely look into the tool you linked to. Really appreciate the help.
|

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.