angularjs - mongoose: accessing formData -
this question has answer here:
- expressjs angularjs post 2 answers
i'm building geolocationapi , i'm trying query locations based on min , max distance submitted user. however, i'm not being able read values in controller.
my input:
<div class="form-group"> <label>min distance</label> <input type="text" class="form-control text-center" ng-model="mindistance"><br> </div> <div class="form-group"> <label>max distance</label> <input type="text" class="form-control text-center" ng-model="maxdistance"><br> </div> i tried this, send controller:
$scope.searchlocations = function () { $scope.formdata.mindistance = $scope.mindistance; $scope.formdata.maxdistance = $scope.maxdistance; $http.get('/api/places') .success(function (data) { $scope.searchlocations = data; //console.log($scope.formdata.mindistance); }) .error(function (data) { console.log('error: ' + data); }); } and on controller have this:
exports.find_all_locations_near = function(req, res) { if (req.body.mindistance > 0) { location.find({ geolocation: { $near : { $geometry: { type: "point", coordinates: [ lat, lon ] }, $mindistance: req.body.mindistance, $maxdistance: req.body.maxdistance } } }), function(err, location) { if (err) { //console.log(err); res.send(err); } else if (location =='') //res.json({ message: 'no location found.' }); res.json({ message: 'no location found.' }); else { res.json(location); res.json({ message: 'the end.' }); } }; } else { location.find({}, function(err, location) { if (err) res.send(err); res.json(location); //res.json(req.body); }); } } i assume cant use req.body way, right? how should access value of min , max distance?
a request not have body.
to pass search values, can use query string parameters.
we can write function serialize set of named values form query string send our backend.
function withquery(url, parameters) { const query = object.keys(parameters) .map(function (key) { return key + '=' + parameters[key]; }) .join('&'); return query.length ? url + '?' + query : url; } we leverage function in modified search method below
$scope.searchlocations = function () { const url = withquery('api/places', { mindistance: $scope.mindistance, maxdistance: $scope.maxdistance }); $http.get(url) .then(function (response) { $scope.searchlocations = response.data; //console.log($scope.formdata.mindistance); }) .catch(function (reason) { console.log('error: ' + reason); }); }; note using then , catch instead of success , error latter deprecated , have problematic behavior.
in server side code, can access values using query property of request
exports.find_all_locations_near = function(req, res) { const $mindistance = req.query.mindistance; const $maxdistance = req.query.maxdistance; if ($mindistance > 0) { location.find({ geolocation: { $near: { $geometry: { type: "point", coordinates: [lat, lon] }, $mindistance, $maxdistance } } }); // ... }; query string parameters start ? , take form key=value, delimited &. not need declared part of route declaration , implicitly available on query property of express.request value passed callback.
we can verify behavior following snippet
app.use('/test', (req, res) => { res.send(object.entries(req.query) .map(([key, value]) => `${key} ---> ${value}`) .join('<br>') ); }); and navigating /test?x=1&y=hello in our browser render
x ---> 1
y ---> hello
note: although req.params value not populated query parameters, req.param function can called query parameter name value. e.g. const id = req.param('id'). makes api feel inconsistent.
Comments
Post a Comment