NodeJS GET请求不使用AngularJS

Qui*_*oNa 2 javascript node.js express angularjs

我有这个用于分享照片的网络应用程序.

现在我有这条路线应该从以下数组返回所有用户的照片.

路线:

router.get('/getphotos',function(req, res){
    var reqPhotos = [];
    console.log( "\n" + req.body.username + "\n");
    try{
      for(x =0; x < req.body.following.length;  x++){
        reqPhotos.push({username: req.body.following[x].username});
      }
    }
    catch(err){
      console.log(err);
    }

    Photo.find({username: reqPhotos}).exec(function(err, allPhotos){
        if(err){console.log(err);}
        else{
          res.json(allPhotos);
        }
   });

});
Run Code Online (Sandbox Code Playgroud)

我发现req.body.following是未定义的.这是我用angular调用它的方式:

$scope.getPhotos = function(){

    if($scope.identification){
        flng = angular.copy($scope.identification.following);
        flng.push($scope.identification.username);
        var data = {username: $scope.identification.username, token: $scope.identification.token, following: flng}
    //IDENTIFICATION HAS ALL THE INFO.
    $http.get('/users/getphotos', data).success(function(response){
        $scope.photos = response;
    });
    }
}
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种情况以及如何解决?

谢谢!

fik*_*tra 5

不确定服务器端,但我看到角度代码中有两个问题.在执行HTTP GET请求时,您无法传递正文.尝试通过网址传递任何必要的数据.

此外,返回的实际数据将在response.data.做这样的事情:

var urlData = ""; //add any url data here, by converting 'data' into url params
$http.get('/users/getphotos/' + urlData).then(function(response){
    $scope.photos = response.data;
});
Run Code Online (Sandbox Code Playgroud)

要构建urlData,请查看问题.

当然,您必须调整服务器,以便从URL中读取数据,而不是从正文中读取数据.