0

I'm using in bootstrapvue, I have an API that data coming from it, please see codes below, I will explain more:

      <b-pagination
        v-model="currentPage"
        :total-rows="rows"
        :per-page="perPage"
        aria-controls="itemList"
        align="center"
      ></b-pagination>
    export default {
      data() {
        return {
          posts: null,
          perPage: 1,
          currentPage: 1,
        }
      },
  computed: {
    rows() {
      if (this.posts.length >= 1 && this.posts.length !== null) {
        return this.posts.length
      } else {
        return false
      }
    },
    getItemForPagination() {
      return this.posts.slice(
        (this.currentPage - 1) * this.perPage,
        this.currentPage * this.perPage
      )
    },
  },

a bit of what I want to do: as you can see in the code above, I want to have pagination which items mix with it, I mean the number of items that have shown be under the control of pagination. well, when I give rows computed property to :total-rows it returns an error, that I think because API isn't loaded on rows yet, but when writing on the template or console something like this as I did as on computed rows posts.length', everything works fine, but on :total-rows` game me this error:

TypeError: Cannot read property 'length' of null

I'm using nuxtjs

1
  • You should have write down what you did to debbug or/and solve the issue. Commented Dec 31, 2020 at 14:02

1 Answer 1

2

You should initialize your posts property with an empty array :

    data() {
    return {
      posts: [],
      perPage: 1,
      currentPage: 1,
    }
  },

then compare the length to 0 :

 if (this.posts.length >= 1 && this.posts.length !== 0) {

or just do :

 if (this.posts.length) {
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.