0

I am creating an application using mean stack. I need to create a advanced search feature for my application where schema can searched by various fields like firstname, lastname, etc. On clicking the search button I will use the below route to connect to the server.

$http.get('/search/' + $routeParams.searchdata)

now if I try to console log the searchdata I can get the fields that I entered in the search form. Eg: If I enter the first name and the email id I see in the console as

{"firstname": "Smith", "email": "smith@gmail.com"}

If I try to access searchdata.firstname I get undefined.

1) How can I access the fields entered in the search form?

2) There are many fields in the schema. How will I tell to search only the fields that were entered in the form?

1 Answers1

0

1) How can I access the fields entered in the search form?

See this answer Node.js: Difference between req.query[] and req.params

2) There are many fields in the schema. How will I tell to search only the fields that were entered in the form?

Just pass the query to your find mongoose function like this :

app.get('yourPath', function(req, res){
    yourCollection.find(req.query, function(err, elements){
                if(err)
                       res.status(500).send(err);
                else{
                               res.json(elements);
                }
}
Community
  • 1
  • 1
hzitoun
  • 5,492
  • 1
  • 36
  • 43