Core: Setting View global in global middleware is not attaching to current request.

Created on 15 Jun 2017  路  5Comments  路  Source: adonisjs/core

I have global middleware that builds an object with user data (and other stuff later).
Here is an example: http://prntscr.com/fk710w
.......
Also I've tested this bug with random number: http://prntscr.com/fk71xi
In my master.njk view:

<pre>The num is: {{ randomNumber }}</pre>

Scenario:

  • First load: console shows that number is 1050, in view I have nothing (null)
  • Reload: console shows that number is 2952, but now in view I see 1050 (previously generated number)
  • Reload: console shows 9999, in view number is 2952 (again previously generated number)
  • etc...

Adonis: v3.0.10
NPM: 3.10.10
Node: v6.10.1

There is something messing up with an old request data?
How to get this thing working?
Thanks.

Most helpful comment

I suggest u to set globals on the view instance of response object

response.viewInstance.global()

All 5 comments

http://prntscr.com/fk8ojl -okay, now everything is fine. but it sucks a little bit.. :D
So maybe there is better solution to avoid this bug?

Hey @NViktors 馃憢

You must use response.viewInstance.global.

Example:

* handle (request, response, next) {
  response.viewInstance.global('currentUrl', request.url())
  response.viewInstance.global('currentOriginalUrl', request.originalUrl())

  yield next
}

I suggest u to set globals on the view instance of response object

response.viewInstance.global()

Woow, thank you! now it works. :))

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings