express.JS .push cousing数组未定义

时间:2016-03-10 11:25:30

标签: javascript angularjs node.js express

嗨,出于某种原因,每当我尝试将数据推送到我的数组时,我得到一个错误,返回说数组是未定义的

         function getPosts(initial){


        var data = {};

         if ($scope.user){

           data.ids = angular.copy($scope.user.friends);
           data.ids.push($scope.user._id)

        }

        $http.post('api/social/getPost', data).success(function(response) {

            if (initial) {

                $scope.wallposts = response;

                if (response.length == 0) {

                    getPosts(true);

                } else {

                    $scope.wallposts = response;

                }

            } else {

                if (response.length > $scope.wallposts.length) {

                    $scope.IncomingPosts = response;

                }

            }

        });

    };

这是错误

Error: data.ids is undefined
getPosts@http://localhost:3000/client/controllers/PostController.js:48:16
@http://localhost:3000/client/controllers/PostController.js:105:9
invoke@http://localhost:3000/node_modules/angular/angular.js:4604:16
$ControllerProvider/this.$get</</instantiate<@http://localhost:3000/node_modules/angular/angular.js:9855:24
nodeLinkFn@http://localhost:3000/node_modules/angular/angular.js:8927:34
compositeLinkFn@http://localhost:3000/node_modules/angular/angular.js:8226:13
compositeLinkFn@http://localhost:3000/node_modules/angular/angular.js:8229:13
compositeLinkFn@http://localhost:3000/node_modules/angular/angular.js:8229:13
compositeLinkFn@http://localhost:3000/node_modules/angular/angular.js:8229:13
nodeLinkFn@http://localhost:3000/node_modules/angular/angular.js:8973:1
compositeLinkFn@http://localhost:3000/node_modules/angular/angular.js:8226:13
publicLinkFn@http://localhost:3000/node_modules/angular/angular.js:8106:30
compilationGenerator/<@http://localhost:3000/node_modules/angular/angular.js:8447:20
createBoundTranscludeFn/boundTranscludeFn@http://localhost:3000/node_modules/angular/angular.js:8244:1
controllersBoundTransclude@http://localhost:3000/node_modules/angular/angular.js:9020:20
ngIfWatchAction@http://localhost:3000/node_modules/angular/angular.js:25059:15
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:3000/node_modules/angular/angular.js:16664:23
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:3000/node_modules/angular/angular.js:16928:13
done@http://localhost:3000/node_modules/angular/angular.js:11266:36
completeRequest@http://localhost:3000/node_modules/angular/angular.js:11464:7
requestLoaded@http://localhost:3000/node_modules/angular/angular.js:11405:1

但是,如果我删除将id推入数组的行,一切正常吗?

服务器端的代码是

module.exports.getPosts = function(req, res){

//get all friends and users posts
Posts.find( {postedBy: {$in: req.body.ids}} )
    .sort({postedOn: -1})
    .exec(function(err, allPosts){

        if (err) {

            console.log(err)

        } else {

            res.json(allPosts)

        }
    });

};

我想要做的就是从用户朋友那里收集所有ID然后将用户ID添加到数组中,这样我就可以使用$ in查询来搜索mongo以查找由他们创建的所有帖子。

我花了5天时间来处理这个错误,说实话我不知道发生了什么

这是客户端的完整代码,以防它帮助

(function(){

    angular.module('Scrimbox')

     .controller('postsController', ['$scope', '$http', '$interval', '$routeParams', function( $scope, $http, $interval, $routeParams){


        $scope.newPost = function(){

            var request = {};

            var user = JSON.parse(localStorage.getItem("User-Data"));
            var userId = user["_id"];
            var useravatar = user["avatar"];
            var username = user["username"];

                var request = {

                    postedBy: userId,
                    posts_avatar: useravatar,
                    username: username,
                    content: $scope.postContent

                };


            //send to server
            $http.post('api/social/newPost', request).success(function(response){


                  getPosts(true);

            }).error(function(error){

                console.log(error);

            });
        };

         function getPosts(initial){


            var data = {};

             if ($scope.user){

               data.ids = angular.copy($scope.user.friends);
               data.ids.push($scope.user._id)

            }

            $http.post('api/social/getPost', data).success(function(response) {

                if (initial) {

                    $scope.wallposts = response;

                    if (response.length == 0) {

                        getPosts(true);

                    } else {

                        $scope.wallposts = response;

                    }

                } else {

                    if (response.length > $scope.wallposts.length) {

                        $scope.IncomingPosts = response;

                    }

                }

            });

        };

         $interval(function(){

            getPosts(false);

            if ($scope.IncomingPosts) {

                $scope.difference = $scope.IncomingPosts.length - $scope.wallposts.length;

            }
             console.log("this is working");

         }, 5000);


        $scope.newP = function(){

            console.log('getting new posts');

            $scope.wallposts = angular.copy($scope.IncomingPosts);
            $scope.IncomingPosts = undefined;
        }

        //Init
        getPosts(true);

    }]);
}());

1 个答案:

答案 0 :(得分:0)

Molda的回答。

  

所以也许$ scope.user.friends第一次未定义   angular.copy将undefined分配给data.ids,快速修复可以在之后   data.ids = angular.copy($ scope.user.friends);如果(!data.ids ||,请执行此操作   !data.ids.length)data.ids = []