express.JS .push cousing array to be undefined

Hi for some reason everytime i try to push data into my array i get a error returned saying the array is undefined

         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;

                }

            }

        });

    };

this is the error

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

however if i remove the line that pushed the id into the array everything works fine?

the code on the server side is

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)

        }
    });

};

all i am trying to do is gather all ids from the users friend then add the users id to the array so i can use a $in query to search mongo for all posts that have been created by them ids.

i have spent 5 days on this bug and to be honest i have no idea what is going on

here is the full code for the client side in case it helps

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

    }]);
}());

Answers


Answers by Molda.

So maybe $scope.user.friends is undefined the first time so angular.copy assigns undefined to data.ids, quick fix could be after data.ids = angular.copy($scope.user.friends); do this if(!data.ids || !data.ids.length) data.ids = []


Need Your Help

Convert Mouse Points to Quadratic BSplines

c++ c opengl

I'm writing a drawing program. I'm trying to take an ordered list mouse positions, and approximate a smooth Quadratic BSpline Curve. Does anyone know how to accomplish this?

Making a supervisor process in C

c supervisor

I want to make a supervisor process in c which controls the startup of some other processes, keep an eye on all the processes (previously started by itself) and deal with any error like if any of the