View globals errors


#1

Hi.

I’m trying to create a new global for my views, but I always get the error message

{
error: [
{
error: "Cannot call function button from dashboard.edge view",
code: 500,
string_id: "",
params: [ ],
field: ""
}
],
data: { }
}

For now i’ve just copy pasted from the docs, but its not working. Is there something I’m forgetting?

start/hooks.js:

const { hooks } = require('@adonisjs/ignitor')


const moment = require('moment')

hooks.after.providersBooted(() => {
    const View = use('View')

    View.global('currentTime', function () {
        return new Date().getTime()
      }) }

And then i try to use in one of my views like this {{ currentTime() }}


#2

Hey @torgeirbeyer! :wave:

Your issue must be in your template.
Could you share the code of dashboard.edge?


#3

its basically like this:

@layout('layouts.master)
@section('main')
    lot of html
    some @!component('components.some-component')
@endsection

its inside the main section i try to call the function


#4

Looking at the error, it seems that you are trying to call a function button that doesn’t exist.

Do you know where that happens?


#5

oh, yeah the error is just from another global function i was trying out. also copy pasted from the docs btw. @romain.lanz


#6

Any new thoughts on this @romain.lanz? Or someone else?


#7

Can you share actual code? So far there is no reference to the button function or the function that calls it


#8

Sure.
start/hooks.js

const { hooks } = require('@adonisjs/ignitor')

const moment = require('moment')

hooks.after.providersBooted(() => {
    const View = use('View')

    View.global('currentTime', function () {
        return new Date().getTime()
      })

    View.global('moment', function (time, unit = 'X') {
        return moment(time, unit)
    })

    View.global('onoffBadge', function (status, yesno = false) {
        if (status === 'T')
            return '<span title="På" class="badge badge-success">' + (yesno ? 'Ja' : 'På') + '</span>';
        else
            return '<span title="Av" class="badge badge-danger">' + (yesno ? 'Nei' : 'Av') + '</span>';

    })

    View.global('button', function (text) {
        return this.safe(`<button type="submit"> ${text} </button>`)
      })
})

dashboard.edge:

@layout('layouts.master')


@section('main')
    <article class="c-article">
        <header class="c-article__header">
            <div class="container">
                <div class="row">
                    <div class="col-sm-7">
                        <h1>{{ antl.formatMessage('dashboard.title') }}</h1>
                        <p class="font-weight-lighter">{{ antl.formatMessage('dashboard.greeting', {name: auth.user.username})}}</p>
                    </div>
                    <div class="col-sm-4">
                        <div id='span-links' class="btn-group float-right">
                            <a id='total' href="/" class="btn btn-primary">{{ antl.formatMessage('dashboard.total') }}</a>
                            <a id='year' href="/?span=year" class="btn btn-primary">{{ antl.formatMessage('dashboard.year') }}</a>
                            <a id='month' href="/?span=month" class="btn btn-primary">{{ antl.formatMessage('dashboard.month') }}</a>
                            <a id='week' href="/?span=week" class="btn btn-primary">{{ antl.formatMessage('dashboard.week') }}</a>
                            <a id='day' href="/?span=day" class="btn btn-primary">{{ antl.formatMessage('dashboard.day') }}</a>
                        </div>
                    </div>
                    <div class="col-sm-1">
                            <p class="nav-links">Velg språk:</p>
                            @each(locale in locales)
                                <a class="label link" href="/switch/{{ locale }}"> {{ locale }} </a>
                            @endeach
                    </div>
                </div>
            </div>
        </header>

        <div class='container'> 
           {{--   
           <div class='row d-flex '>
             @!component('components.dash-dropdown-chains', chains=chains)
             @!component('components.dash-dropdown-centers', centersInChain=centersInChain)
            </div> 
             --}}
        </div>
        <div class="container">
            <div class="row">
                <p>{{currentTime()}}</p>
            </div>
        </div>

        @!component('components.door-statistics', stats = stats, span_text=span_text, antl=antl)
        
        <section class="c-article__section">
            <div class="container">
                <div class='row'> 
                    <h2 class='ml-2'>{{antl.formatMessage('dashboard.active')}}</h2>
                    <div class="c-link-group medium col-sm-4 d-flex align-items-center">
                        <a class="active" href="/kampanjer/utfordringer">{{antl.formatMessage('dashboard.see_all')}}</a>
                    </div>
                </div>
                {{--  fix issues with campaign and challenges names  --}}
                @!component('components.dash-challenges', challenges = challenges, antl=antl)
            </div>
        </section>
        <section class="c-article__section">
            <div class="container">
               <div class='row'> 
                    <h2 class='ml-2'>{{antl.formatMessage('dashboard.last')}}</h2>
                    <div class="c-link-group medium col-sm-4 d-flex align-items-center">
                        <a class="active" href="/kampanjer">{{antl.formatMessage('dashboard.see_all')}}</a>
                    </div>
                </div>
                @!component('components.dash-challenges', challenges = contents, antl=antl)
            </div>
        </section>
    </article>
@endsection

error message:

{
error: [
{
error: "Cannot call function currentTime from dashboard.edge view",
code: 500,
string_id: "",
params: [ ],
field: ""
}
],
data: { }
}

#9

Hi again. Anything new on this? @virk @romain.lanz


#10

Looking at your code I don’t find any issue.
Could you share a small repository that reproduces the issue?