None
None

Reputation: 9247

Do something after response is finished?

What im trying to do is add in array some data after response is finished. Im trying to check if reponse is ready but without success:

this.$http.post('/blog/article/' + articleid + '/comment', article_comments)
.then(function (response) {
    self.comment_id = response.data.comments.id;
    this.user = response.data.comments.user;
    this.dataReady = true;
  }, function (response) {

});

if (this.dataReady == true) {
  this.comments.push({
      comment: this.comment,
      downvotes: 0,
      upvotes: 0,
      user:this.user,
      date_ago: moment(Date.now()).fromNow()
    })
  this.loadComments();
  console.log(this.comments);
}

How can i fix this? Because i need data from response and then to push in array, or i will get an error if im trying to push in array before reponse is finished.

Upvotes: 1

Views: 367

Answers (1)

Saurabh
Saurabh

Reputation: 73609

You can put following code, which you want to execute after getting the response in a method: say updateOtherVars:

if(this.dataReady == true){
        this.comments.push({
            comment: this.comment,
            downvotes: 0,
            upvotes: 0,
            user:this.user,
            date_ago: moment(Date.now()).fromNow()

          })
            this.loadComments();
              console.log(this.comments);
      }

and call this method from the this.$http block, like following:

this.$http.post('/blog/article/' + articleid + '/comment', article_comments).then(function(response){
          self.comment_id = response.data.comments.id;
          this.user = response.data.comments.user;
          this.dataReady = true;
          this.updateOtherVars()   //Call method from here
        },function(response){

      });

Upvotes: 1

Related Questions