angularjs - Search MongoDB using a variable sent from client -


i'm using mongojs in node project angular , trying find documents based on variable.

server.js

app.get('/api/find', function(req, res){      db.fruits.find({code:'apple'}).foreach(function(err, docs){         res.json(docs);     }); }); 

route

.when('/find/:fruit', {             templateurl: 'views/find.html',                   controller: 'findctrl',             resolve: {                 result: function(searchservice) {                     return searchservice.getresult();                 }             }         }) 

service.js (client)

.factory('searchservice', function($q, $http) {         return {             getresult: function() {                 return $http.get('/api/find')                     .then(function(response) {                         if (typeof response.data === 'object') {                             return response.data;                         } else {                             return $q.reject(response.data);                         }                     })             }               }              }) 

controller.js

.controller('findctrl', function($scope, result) {     $scope.result = result; }) 

as can see in server.js, i'm passing static string 'apple'. wanna replace $routeparams ':fruit' i've listed in route.

please help.

you need pass url param :fruit searchservice:

.when('/find/:fruit', {     templateurl: 'views/find.html',           controller: 'findctrl',     resolve: {         result: function(searchservice) {             return searchservice.getresult($route.current.params.fruit);         }     } }) 

search service:

.factory('searchservice', function($q, $http) {     return {         getresult: function(fruit) {             return $http.get('/api/find?fruit=' + fruit)                 .then(function(response) {                     if (typeof response.data === 'object') {                         return response.data;                     } else {                         return $q.reject(response.data);                     }                 })         }           }          }) 

server: use mongoose use .toarray using foreach iterating on collection , attempting end response multiple times.

app.get('/api/find', function(req, res){      db.fruits.find({code:req.query.fruit}).toarray(function (err, result) {         return res.json(result);     }); }); 

edit: read you're using mongojs should able do:

app.get('/api/find', function(req, res){      db.fruits.find({code:req.query.fruit}, function (err, docs) {         return res.json(docs);     }); }); 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -