Я пытаюсь написать приложение, которое находит город в коллекции MongoDB и использует широту и долготу, которые он возвращает, чтобы найти все почтовые индексы на определенном расстоянии. Кажется, что это работает, но проблема в том, что я получаю сообщение об ошибке, что я не могу установить заголовки после их отправки. Тем не менее, я отделил маршруты до разных запросов, я не понимаю, почему я все еще получаю эту ошибку. Каков наилучший способ сделать несколько вызовов API?NodeJS - Невозможно установить заголовки после их отправки - Несколько вызовов
Вот мой маршрутизатор в узле/Экспресс:
// route to get city
app.get('/cities/:zip', function(req, res) {
// use mongoose to get the city in the 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 to 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
});
Вот мой Угловая контроллер
$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 (ERR),' 'для возврата res.send (ERR)'? Также на несвязанной ноте ваш угловой код имеет 'zip.length = 5' вместо' zip.length === 5'. – mscdex