Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/423.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript angularjs:Push不是一个函数_Javascript_Angularjs_Json - Fatal编程技术网

Javascript angularjs:Push不是一个函数

Javascript angularjs:Push不是一个函数,javascript,angularjs,json,Javascript,Angularjs,Json,我有一个JSON对象,如下所示: var post = { "post_id": "1", "content": "content", "post_author": { "id": "12", "firstName": "Amelia", "lastName": "Earheart", }, "isLiked": false, "likes_count": 0, "likers": [],

我有一个JSON对象,如下所示:

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": []
};
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;
    });
};
{"id":"12","firstName":"Amelia","lastName":"Earheart"}
post
从前端传递到下面给出的函数

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;
    });
};
但是这样做,我得到一个JavaScript错误,说
push不是
likedPost.likers.push(userObject)行中的函数

vm.userInfo()
返回
userObject
,如下所示:

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": []
};
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;
    });
};
{"id":"12","firstName":"Amelia","lastName":"Earheart"}
返回的JSON如下所示:

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": []
};
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;
    });
};
{"id":"12","firstName":"Amelia","lastName":"Earheart"}
有人能帮我找出这个问题的原因吗

更新:

{
    "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,
}

这就是我得到的
console.log(类似于Post)

输出明确指定未定义
likers
。您可以在使用
push()
方法之前进行验证检查

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

您的
likedPost
对象没有您期望的
likers
数组。在尝试推之前,您可能会看到是否存在这种情况

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

在push调用之前,可以添加以下内容:console.log(likedPost);并发送结果。给我一分钟@silvinust此错误意味着
likers
不是数组。JSON对象的源是什么?也许在构造它或接收它时有错误。你可以简单地把(如果喜欢Post)放进去,因为如果为null或未定义,它将返回false。这就是IE需要快速而痛苦地死去的原因