Skip to content
Discussion options

You must be logged in to vote

Hey there! Unfortunately, there isn't a great way within JavaScript to make the automatic retrieval of an asynchronous property work as it can in Spring.

Instead, you'll need to preload (eager) or load (lazy) the relationship, then you can access its properties.
https://lucid.adonisjs.com/docs/relationships#lazy-load-relationships

For example:

export default class UserPolicy extends BasePolicy {
  async before(user: User | null) {
    if (user === null) return

    await user.load('roles')

    if (user.roles.some((role) => role.code === 'admin')) {
      return true
    }
  }

  // ...
}

Edit: Adding async to example before method

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Xstoudi
Comment options

@thetutlage
Comment options

Answer selected by Xstoudi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
3 participants