0

I realize this might be a dupe, but no question out there that I could find had an answer that solved my problem.

I have a function to add a comment to a news article.

//Post a comment on an event.
$scope.commentEvent = function(event, callback) {
    $scope.user = userService.get({id: $rootScope.current_user_id}, function(){
        event.newComment.event = event._id;
        event.newComment.author = $rootScope.current_user;
        event.newComment.author_id = $rootScope.current_user_id;
        event.newComment.author_portrait = $scope.user.portrait;
        //Save the comment to db
        eventCommentService.save(event.newComment, function() {
            //Add reference to new comment to the news article.
            eventService.update({id: event._id, comments: event.comments.push(event.newComment)}, function() {
                event.newComment = null;
                //update scope and return it
                return $scope.getEventComments(event)
            })              
        });
    }); 
};

//Get all comments related to a specific event.
$scope.getEventComments = function(event) {
    return eventCommentService.query({id:event._id});

}

This is how I use the function in the view. Comments is a scope variable that I loop over with ngRepeat.

<form ng-Submit="comments = commentEvent(event)">

My problem is that commentEvent(event) does not return the updated scope, since the return is in a nested function. How do I return a value from a nested callback function?

Flux
  • 410
  • 1
  • 5
  • 19
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – baao Dec 21 '16 at 17:43
  • what does your **$rootScope** object contains! add the complete controller along with the rootScope details and error in the console ? – Aravind Dec 21 '16 at 17:45
  • Asynchronous APIs can't return values from the future. They can only return promises (a container for a future value). – georgeawg Dec 21 '16 at 20:36

0 Answers0