planety / prologue

Powerful and flexible web framework written in Nim
https://planety.github.io/prologue
Apache License 2.0
1.23k stars 47 forks source link

Type mismatch error #226

Open smalloff opened 11 months ago

smalloff commented 11 months ago

I have problems with the handler. When I try to call some functions in the body of the handler, I get an error. How to deal with it?

My code:

`import prologue import fastcmspkg/utils import prologue/core/uid import tinypool/sqlitePool import db_connector/db_sqlite import tinypool/core import fastcmspkg/templates/nwt

var templates: Nwt

proc hello*(ctx: Context) {.async.} = var s: string

s = templates.renderTemplate("main.html", %*{"faa": "super"}) # If removed, the problem will disappear. resp "Main page from plugin!" & genUid() & ctx.request.hostName() & s

proc Init(app: Prologue, db_pool: ConnectionPool[DbConn], templ: Nwt){.exportPlugin.} = SQLITE_POOL = db_pool templates = templ echo "init plugin " & instantiationInfo().filename app.get("/", hello) # this error!`

My console log:

d:\nim-2.0.0\projects\fastcms\src\plugins\main page\main_page.nim(32, 6) Error: type mismatch Expression: get(app, "/", hello) [1] app: Prologue [2] "/": string [3] hello: proc (ctx: Context): Future[system.void]

Expected one of (first mismatch at [position]): [1] func get(env: Env; key: string): string [1] proc get(group: Group; route: string; handler: HandlerAsync; name = ""; middlewares: openArray[HandlerAsync] = @[]) [1] proc get[T](self: Option[T]): lent T [1] proc get[T](self: Option[T]; otherwise: T): T [1] proc get[T](self: var Option[T]): var T [3] proc get(app: Prologue; route: string; handler: HandlerAsync; name = ""; middlewares: openArray[HandlerAsync] = @[])

mrtlsc commented 4 months ago

I had the same problem. Proc hello has to be of type HandlerAsync, which is proc(ctx: Context): Future[void] {.closure, gcsafe.}. hello is not GC-safe, because it accesses a global variable templates. At least that's what the compiler should say if you add .gcsafe pragma to the proc.