fastify / point-of-view

Template rendering plugin for Fastify
MIT License
344 stars 87 forks source link

view not rendering ejs and keep getiing FST_ERR_REP_ALREADY_SENT error #327

Closed wbrown766 closed 2 years ago

wbrown766 commented 2 years ago

Prerequisites

Fastify version

4.0.3

Plugin version

7.0.0

Node.js version

16.15.1

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Kernel verion 5.4.0-120-generic x86_64 bits

Description

So I have a server set up and and I use ejs templating to render dynamic pages. All worked on fastify v3.29.0 with point-of-view v5.3.0 befor I upgraded to v4.0.3 and v7.0.0 respectively. I was looking to to try out ajv v8 hence the upgrade but anyway.

My the view pluging is autoloaded and I call reply.render to render ejs when needed but i get an FST_ERR_REP_ALREADY_SENT error ever time.

All other routes that return static content work fine. i.e. via @fastify/static or reply.send(JSON.stringify(...data...))

Steps to Reproduce

My the view pluging is autoloaded and looks like this

const fastifyPlugin = require('fastify-plugin');
const ejs = require('ejs');
const path = require('path');
const view = require('@fastify/view');

const config = require('../moduals/config');

const pluginConfig = {
    'engine': {
        'ejs': ejs,
    },
    'root': path.join(__dirname,'../views'),
    'viewExt': 'ejs',
    'propertyName': 'render',
    'defaultContext': {
        'env': process.env.NODE_ENV,
        'info': config.info
    }
};

module.exports = fastifyPlugin(async function (fastify, opts, done) {
    fastify.register(view, pluginConfig);
    done();
});

the problem is every time i call reply.render(...) it outputs this error

{"level":30,"time":1655609102238,"pid":129206,"hostname":"localhost","reqId":"req-1","req":{"method":"GET","url":"/login","hostname":"localhost:8443","remoteAddress":"127.0.0.1","remotePort":55558},"msg":"incoming request"}
{"level":10,"time":1655609102241,"pid":129206,"hostname":"localhost","reqId":"req-1","msg":"fastify-secure-session: there is no session or the session didn't change, leaving it as is"}
{"level":30,"time":1655609102243,"pid":129206,"hostname":"localhost","reqId":"req-1","res":{"statusCode":200},"responseTime":4.766998998820782,"msg":"request completed"}
{"level":40,"time":1655609102246,"pid":129206,"hostname":"localhost","reqId":"req-1","err":{"type":"FastifyError","message":"Reply was already sent.","stack":"FastifyError: Reply was already sent.
    at _Reply.Reply.send (/home/username/project/node_modules/fastify/lib/reply.js:113:26)
    at _readCallback (/home/username/project/node_modules/@fastify/view/index.js:294:12)
    at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read_file_context:68:3)","name":"FastifyError","code":"FST_ERR_REP_ALREADY_SENT","statusCode":500},"msg":"Reply already sent"}
{"level":30,"time":1655609102495,"pid":129206,"hostname":"localhost","reqId":"req-2","req":{"method":"GET","url":"/favicon.ico","hostname":"localhost:8443","remoteAddress":"127.0.0.1","remotePort":55558},"msg":"incoming request"}
{"level":10,"time":1655609102497,"pid":129206,"hostname":"localhost","reqId":"req-2","msg":"fastify-secure-session: there is no session or the session didn't change, leaving it as is"}
{"level":30,"time":1655609102500,"pid":129206,"hostname":"localhost","reqId":"req-2","res":{"statusCode":200},"responseTime":5.079601000994444,"msg":"request completed"}

I tried taking out the propertyName and calling reply.view but got same error

Expected Behavior

No response

climba03003 commented 2 years ago

Please check https://medium.com/@fastifyjs/fastify-v4-ga-59f2103b5f0e

You need explicit return of reply in async handler.

If you believe it is not the same issue or cause. Please provide a minimal repo that can run directly. You current example missing too many pieces like ../moduals/config is unknown to me. There is no call of reply.render.

wbrown766 commented 2 years ago

Sorry complety missed that. Didn't even reaslie it was related as I was get a FST_ERR_REP_ALREADY_SENT not a FST_ERR_PROMISE_NOT_FULFILLED error.

It was rather late las night... SORRY... works fine now.