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

Node.js: Internal Server error with Mongoose

本文关键字:服务器 错误 内部 Mongoose js Node      更新时间:2023-09-26

我有一个用于共享照片的网络应用程序。

现在我有了这个路由,它应该返回following阵列中所有用户的照片。

路线:

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

但是,它给了我一个内部服务器错误。我从服务器上得到一个500

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

谢谢!

编辑:

我发现下面的req.body.没有定义。这就是我用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;
            });
            }
        }

尝试使用:

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