angularjs - NodeJS - Cannot set headers after they are sent - Multiple Calls -


i'm trying write app find city in mongodb collection , uses latitude , longitude returns find zip codes within distance. seems work, problem i'm getting error can't set headers after they've been sent. however, i've separated routes different requests don't understand why i'm still getting error. best way make multiple calls api?

here router in node/express:

// route city app.get('/cities/:zip', function(req, res) {     // use mongoose city in database     console.log(req.params.zip);     var query = city.find({"zip" : req.params.zip});     query.exec(function(err, city) {         if (err)             res.send(err);         res.json(city);     }); });  // route find cities within 50 miles app.get('/matches/:latmin/:latmax/:lonmin/:lonmax', function(req, res) {     console.log(req.params.latmin + req.params.latmax + req.params.lonmin + req.params.lonmax);     var matches = city.find({latitude: {$gt: req.param.latmin, $lt:req.params.latmax }, longitude : {$gt :req.param.lonmin, $lt : req.param.lonmax}});     matches.exec(function(err, match){         if(err)             res.send(err);         console.log(match);         res.json(match);     });      });  app.get('*', function(req, res) {     res.sendfile('./public/views/index.html'); // load our public/index.html file }); 

here angular controller

$scope.update = function (zip) {     city.get({zip : zip}).success(function(response){         $scope.weather = response     }).then(function(response){         $scope.weather = response.data;     })      if(zip.length = 5){         $http.jsonp('http://api.openweathermap.org/data/2.5/weather?zip='+ zip +',us&callback=json_callback&units=imperial').success(function(data){             $scope.data=data;         });           var box = getboundingbox([$scope.weather[0].latitude, $scope.weather[0].longitude], 50);         city.matches(box[1], box[3], box[0], box[2]).success(function(response){             $scope.matches = response         }).then(function(response){             $scope.matches = response.data;             console.log($scope.matches);         })     } 

res.send not return; call continues res.json. , please use braces. please. maybe don't cool or whatever. use them.

if (err) { handleerror(res, err); return; } res.status(200).json(city); 

further down, keeping things dry:

function handleerror(res, err) {   res.status(500).json(err); }     

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -