I'm using MongoJS in a Node project with Angular and trying to find documents based on a 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 you can see in server.js, I'm passing a static string 'Apple'. I wanna replace that with the $routeparams ':fruit' which I've listed in Route.
Please help.