NodeJS - 无法在发送标头后设置标头 - 多个呼叫

时间:2015-06-14 01:15:13

标签: angularjs node.js mongodb

我正在尝试编写一个在MongoDB集合中查找城市的应用程序,并使用它返回的纬度和经度来查找特定距离内的所有邮政编码。它似乎工作,但问题是我收到一个错误,我已经发送后无法设置标题。但是,我已将路由分成不同的请求,我不明白为什么我仍然会收到此错误。多次调用API的最佳方法是什么?

这是Node / Express中的路由器:

// 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);
        })
    }

1 个答案:

答案 0 :(得分:2)

#List没有回来;通话继续$('#bodyRead #List').off('click').on('click', function() ... $('#bodyWrite #List').off('click').on('click', function() ... 。请使用大括号。请。也许他们看起来不酷或其他什么。只需使用它们。

res.send

进一步向下,保持干燥:

res.json
相关问题