0

I am trying to access data value reviews into scroll method . Consoled reviews by console.log(this.reviews). but it always returns undifined;

data() {
  return {
    m: '',
    rating: '',
    review: '',
    visibility: '',
    reviews: [],
    page: 1,
  };
},



scroll (page) { 
  $('#profile-edits-more').on("scroll", function() {      
    if(jQuery(this).scrollTop() + jQuery(this).innerHeight() >= this.scrollHeight) { 
      var vm = this;
      axios.post('./fetch-reviews?page='+page, {
        m: vm.m,
      })
      .then(response => {
        page +=1; 
        console.log(this.reviews);
      });       
    }
  });
},

1 Answer 1

1

Wrong this try:

scroll (page) { 
  var vm = this;
  $('#profile-edits-more').on("scroll", function() {      
    if(jQuery(this).scrollTop() + jQuery(this).innerHeight() >= this.scrollHeight) { 
      axios.post('./fetch-reviews?page='+page, {
        m: vm.m,
      })
      .then(response => {
        vm.page += 1; 
        console.log(vm.reviews);
      });
    }
  });
},

UPD:

$('#profile-edits-more').on("scroll", ===> function() <=== {

Expression function() ... creates a new scope (with new this). You can also use arrow-methods for such things:

scroll(page) { 
  let $el = $('#profile-edits-more');
  $el.on('scroll', () => {
    if ($el.scrollTop() + $el.innerHeight() >= $el.scrollHeight) { 
      axios.post('./fetch-reviews?page='+page, {
        m: this.m,
      })
      .then(response => {
        this.page += 1; 
        console.log(this.reviews);
      });
    }
  });
},
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.