0

I want to run getComments() function inside an object value

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);
                });
            },

And below is getComments function

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

The getComments function is executed successfully, the request is executed successfully with data responses, but it fail to return the data to the caller. How can I return the data to the caller?

  • This is an educated guess: does this framework you use have read only properties? Are you certain that you're running it? You could your getComments function before the object (right after your if(response.data)) and store the return in a variable just to be sure everything is working as expected. – Vic Wolk Oct 23 '17 at 15:58
  • @VicWolk Yes, the function is executed successfully. I can't run the function as you mentioned, because I need to pass `data.id` parameter that I got from the `foreach` and pass it to `getComments` function – Mohamad Nasir Oct 23 '17 at 16:14
  • Yes, I know. It was just for the sake of testing and logging the return of the first id in data. I'm sorry I didn't complete that thought. You can also do that inside the foreach, before the push, then assign it to the comments property. Again, just for the sake of testing. – Vic Wolk Oct 23 '17 at 16:19
  • @VicWolk I have added `console.log(comment)` and `console.log(theComments)` and the data is there, but somehow it can't write to the caller. – Mohamad Nasir Oct 23 '17 at 16:28
  • What if you hammer down a static array to the comments property, just to check whether it's sending it forward? How can you tell it's not writting to the caller? Are you checking it in a backend or something? – Vic Wolk Oct 23 '17 at 16:36

0 Answers0