3

I have a JSON object like this:

var post = {
    "post_id": "1",
    "content": "content",
    "post_author": {
        "id": "12",
        "firstName": "Amelia",
        "lastName": "Earheart",
    },
    "isLiked": false,
    "likes_count": 0,
    "likers": [],
    "comments_count": 0,
    "commenters": [],
    "comments": []
};

And post is passed to the function given below from the front end.

var vm = this;
vm.likePost = function(post) {
    var likedPost = post;
    vm.userInfo();
    likedPost.likers.push(userObject); //Here

    myService.postLike(likedPost).success(function(data) {
        likedPost.isLiked = true;
        likedPost.likes_count++;
        vm.posts = data;
    });
};

But doing so, I get a JavaScript error saying push is not a function in line likedPost.likers.push(userObject);

And userObject is returned by vm.userInfo() and it looks like this:

vm.userInfo = function() {
    myService.getBasicUserInfo().success(function(data) {
        vm.currentPost.post_author.id = data.id;
        vm.currentPost.post_author.firstName = data.firstName;
        vm.currentPost.post_author.lastName = data.lastName;
    });
};

and the returned JSON is like this:

{"id":"12","firstName":"Amelia","lastName":"Earheart"}

Can anyone help me figure out the cause of this issue?

UPDATE:

{
    "post_id": "12",
    "content": "Content is the content that contains the content",
    "image": "member-default.jpg",
    "created_at": "2016-05-26 14:29:00",
    "post_author": {
        "id": "12",
        "firstName": "Amelia",
        "lastName": "Earheart",
    },
    "isLiked": false,
}

This is what I get upon console.log(likedPost);

4
  • 1
    Before the push call can you add this : console.log(likedPost); and send the result Commented Jun 14, 2016 at 9:24
  • OK. Give me a minute. @Silvinus Commented Jun 14, 2016 at 9:24
  • 1
    This error means that likers is not an array Commented Jun 14, 2016 at 9:24
  • What is the source of your JSON object? Maybe there is an error in constructing it, or in receiving it. Commented Jun 14, 2016 at 10:01

2 Answers 2

7

The output clearly specifies that likers is not defined. You can put a validation check before using push() method.

//if likedPost.likers is not defined, it will define it as an array 
likedPost.likers = likedPost.likers || [];

//Do the push operation
likedPost.likers.push(userObject);
Sign up to request clarification or add additional context in comments.

Comments

0

Your likedPost object doesn't have the likers array you expect. Probably you can see if that exists before trying to push.

if (typeof likedPost !== 'undefined')
   likedPost.likers.push(userObject);

2 Comments

You could simply put (if likedPost) because it would return false if null or undefined
all the reason why IE needs to die a quick and horrifyingly painful death

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.