0

I have a component inside a route-view which is in turn part of another route-view so the tree is like this:

<app>
  <appContainer>  // This is a route-view
    <myView> // Also a route-view
      <myComponent>

In myComponent I have this conditional rendering:

<b-button v-if="isAdmin(currentUser)'">

This function is exhibiting unexpected behaviors

isAdmin(username) {
  let userObject = this.$store.getters.getCurrentUserObject(username)
  return userObject.role === 'ADMIN'
}

The userObject was turning up as undefined until I added a debugger inside the function and realized that this it's being called multiple times during page rendering some of which the store data property is still empty resulting in getters returning null. What could be the reason? Why is this method called multiple times?
PS: I have a for loop in the component, could this be the reason?

<div v-for="bike in bikes":key="bike.timestamp">
  <p>{{ bike.name }}</p>
  <b-img fluid v-if="bike.imagePath" v-bind:src="returnImage(bike.imagePath)"></b-img>
  <p>{{ bike.timestamp}}</p>
</div>  

This is called in the created hook

created() { 
   this.getBikes()
 }
3
  • If there's anything async involved this is expected behavior--things render when things change, and barring a synchronous process, you need to be able to handle data not being set or percolating through the system yet. Commented Apr 25, 2019 at 18:43
  • There are no explicit async operations on the frontend (only backend) though make calls using Axios not sure if that counts. Commented Apr 25, 2019 at 18:47
  • Yes, that counts. Ajax calls are asynchronous. Commented Apr 25, 2019 at 18:59

2 Answers 2

1

Sounds like a return type issue at first sight.

isAdmin is already returning a boolean which in turn is getting “explicitly” compared to currentUser. The result of such comparison is always falsy.

You gotta change the v-if directive’s condition to something like:

v-if="isAdmin(currentUser)"

Regarding the multiple calls, you gotta update your question to reflect the details of the loop you’ve mentioned.

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

2 Comments

You're right about the return and I've modified accordingly but the underlying problem persists.
@Samuel Your update to the question is not helpful at all. Please consider putting the whole component code there; only remove the unnecessary irrelevant parts.
0

PS: I have a for loop in the component, could this be the reason? Probably it's the reason because it renders many times as the loop determine.

Provide your code of the loop to have more information to debug.

3 Comments

I'm not sure how that would be helpful, it's a normal v-for loop.
@Samuel We have no way of knowing how the for loop interacts with the code you've shown. If the component loops over the call then obviously it'll be called multiple times.
Definitely, the loop doesn't affect the function isAdmin. As a workaround you can try v-if="currentUser && isAdmin(currentUser)" to call the function only when currentUser is available.

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.