Global View variables with async/await


#1

Hi, I am trying to send the data from Database to my master layout such as menu items and settings.
The Model returns a promise so I used async/await in the provider, but it’s still now showing the value. [object Promise]
Is there any way to send data from Model to all views.

Here is the code for boot method of my provider:

boot () {
    const View = use('View')
    const Setting = use('App/Models/Setting')

    View.global('settings', async function (key) {
        const settings = await Setting.findBy('key', key)
        return settings
    })
}

#2

It is not possible because views are not async and making views async going to add another layer of complexity.

If views are async, then

  • include has to be async
  • component has to be async.
  • each loop has to be async.

Also you are shooting yourself in the foot, if you are going to access the database everytime you call settings inside the view. Which means if you can access setting 10 times in your views, then 10 database queries will be made.

Whereas you should access the settings once and pass them as reference to the view.