0

This is my script tag

<script>
export default {
  data() {
    return {
      blogs: [],    
    };
  },
  created() {
    this.paginate_total = this.blogs.length / this.paginate; 
  },   
};
</script>

these the response I get in my console

{
   
    "blogs": [
   
        {
            "_id": "63243272c988e721db51de9c",
            
        },
        {
            "_id": "63243cb8a8189f8080411e65",        
        },
      
    ]
}

error i get in my console

Cannot read properties of undefined (reading 'length')

Please what I'm I doing wrong

1
  • I tried to reproduce your erro but I didn't found the error in the code that you posted. Probably the problem is in the way that ypu populate blog. Commented Sep 22, 2022 at 12:09

3 Answers 3

2

Place this line in mounted instead of created

this.paginate_total = this.blogs.length / this.paginate;

because this blog is not available in created yet that's why it is undefined.

Sign up to request clarification or add additional context in comments.

2 Comments

I though that too but I did a code snippet here and works well. The documentation says that in created hook the reactive data is setted up so I don't think is that. Here's my code
Solved it using watch property thank for your time
1

You can't access the object array length in mounted or created lifecycles so I just used watch property to get the object length

watch: {
   blogs(blogs) {
      this.paginate_total = blogs.length / this.paginate;
   }
}

Comments

0

For row counts (as for table pagination etc.) i would use a computed property like:

computed: {
  paginate_total() {
      return this.blogs.length;
  },
}

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.