简体   繁体   English

Node.js:Mongoose 的内部服务器错误

[英]Node.js: Internal Server error with Mongoose

I have this web app that is for sharing photos.我有这个用于共享照片的网络应用程序。

Now I have this route that is supposed to return the photos of all the users from the following array.现在我有这条路线应该从following数组中返回所有用户的照片。

Route:路线:

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

});

However, it gives me an internal server error.但是,它给了我一个内部服务器错误。 I get a 500 from the server.我从服务器得到500

Why does this happen and how to fix it?为什么会发生这种情况以及如何解决?

Thanks!谢谢!

EDIT:编辑:

I have found out that the req.body.following was undefined.我发现 req.body.following 是未定义的。 This is how I was calling it using angular:这就是我使用 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;
            });
            }
        }

try with:尝试:

router.get('/getphotos',function(req, res){
    var reqPhotos = [];
    for(x =0; x < req.body.following.length;  x++){
      reqPhotos.push({username: req.body.following[x].username});
    }
    Photo.find().where('username').in(reqPhotos).exec(function(err, allPhotos){
        if(err){console.log(err);}
        else{
          res.json(allPhotos);
        }
    });

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM