Generate url in event listener


#1

Hello,
I am trying to generate a url from an event listener and all I get is null. I don’t understand what I am doing wrong.
I am not allowed to use Route.url() in listeners?

this is my code

'use strict'

// eslint-disable-next-line no-undef
const Mail = use('Mail')
// eslint-disable-next-line no-undef
const Route = use('Route')

const User = exports = module.exports = {}

User.created = async ({ user, token }) => {
  const activateUrl = Route.url('auth.verify_email', { token: token }, { domain: 'mydomain.com' })
  console.log(activateUrl)
}

#2

Hey @melokki! :wave:

It should work fine.
What’s the output of your console.log?


#3

Hello @romain.lanz :wave: happy new year :slight_smile:

I was looking in the source code and realized that I am passing the domain in the 3rd parameter. That domain is then checked in the routes, and in the routes file I don’t have any domain set and that’s why it return null.
I removed the 3rd parameter and it’s giving me a correct route, but I have to append the domain name myself.
I though there is a way I could generate an absolute url using the Route.url()

I ended up with something like this:

const uri = Route.url('auth.verify_email')
const activateUrl = `${Config.get('app.http.baseUrl')}${uri}?token=${token}`

I could not set GET parameters using the Route.url() so this is all I could think of
If there is any other way, I am open :slight_smile:


#4

That’s perfectly fine!