如何在返回数组的对象值内运行函数

时间:2017-10-23 15:50:41

标签: javascript vuejs2 axios

我想在对象值

中运行getComments()函数
loadFeeds: function () {
   var self = this;
   axios.get('http://localhost:9001/posts', {
   headers: {
      'Authorization': 'Bearer asdasdasdasdasd'
                    }
                }).then(function (response) {
                    if (response.status == 200){
                        if(response.data){
                            response.data.forEach(function (data) {
                                self.timeline.unshift({
                                    postId : data.id,
                                    postTime: data.createdAt,
                                    postPrivacy: data.postPrivacy,
                                    post: data.post,
                                    posterId: self.newUpdate.posterId,
                                    posterName: self.newUpdate.posterName,
                                    posterThumb: '',
                                    reactions:'',
                                    comments: self.getComments(data.id)
                                })
                            });
                        }
                    }
                }).catch(function (error) {
                    console.log(error);
                });
            },

以下是getComments功能

getComments : function (postId) {
                var theComments = [];
                axios.get('http://localhost:8001/posts/'+postId+'/comments', {
                    headers: {
                        'Authorization': 'Bearer asdadasdasdasdadas'
                    }
                }).then(function (response) {
                    if (response.status == 200){
                        if(response.data){
                            response.data.forEach(function (comment) {
                                console.log(comment)
                                theComments.push({
                                    commentId: comment.id,
                                    comment: comment.comment,
                                    commenterId: comment.commenterId,
                                    createdAt: comment.createdAt
                                })
                            });
                            return theComments;
                        }
                    }
                }).catch(function (error) {
                    console.log(error);
                });
            }

getComments函数执行成功,请求使用数据响应成功执行,但无法将数据返回给调用者。如何将数据返回给调用者?

0 个答案:

没有答案