fastify / help

Need help with Fastify? File an Issue here.
https://www.fastify.io/
64 stars 8 forks source link

err: Reply was already sent #436

Closed Adityapanther closed 3 years ago

Adityapanther commented 3 years ago

I am getting error that reply already sent

please check the code

fastify.post('/question', {preValidation: [fastify.jwtVerify]}, async(req, rep)=>{

        const {postType, postTitle, postContent} = req.body;
        const xAuthAccessToken = await req.headers['x-auth-access-token'];
        const decoded = await fastify.jwt.decode(xAuthAccessToken);
        const {_uId, role} = decoded;

        switch(postType){
            case '[text]':
                const postData = {
                    _uId,
                    postTitle,
                    postDate: Date(),
                    postUpdateDate: null,
                    postContent: null,
                    postType,
                }

                const questionAdded = await userQuestionDb.insertOne(postData);
                if(!questionAdded) return {status: "error", msg: "post does not added"};
                const {insertedId} = questionAdded;
                rep.send({insertedId});

            break;
            case '[text,images]': 

            break;
            default:
                console.log("defaults");

                break;

        }

    })

error written below

{"level":40,"time":1620399967136,"pid":2596,"hostname":"HP","reqId":"req-1","err":{"type":"FastifyError","message":"Reply was already sent.","stack":"FastifyError: Reply was already sent.\n at _Reply.Reply.send (C:\\Users\\Aditya_kumar\\Desktop\\Project Live\\internet\\sarkarirace.com\\backend\\node_modules\\fastify\\lib\\reply.js:118:26)\n at Object.<anonymous> (C:\\Users\\Aditya_kumar\\Desktop\\Project Live\\internet\\sarkarirace.com\\backend\\app\\routers\\question.js:27:21)\n at processTicksAndRejections (internal/process/task_queues.js:93:5)","name":"FastifyError","code":"FST_ERR_REP_ALREADY_SENT","statusCode":500},"msg":"Reply already sent"}

mcollina commented 3 years ago

Can you create a reproducible example?

Adityapanther commented 3 years ago

server.js

const fastify = require('fastify')({logger: true});

// importing custom routes

// const authRoute = require('./routers/authentication');
const questionRoute = require('./routers/question');

// importing decorator

const jwtVerify = require('./decorate/jwtAuthVerify');

// importing schema

const dotEnvSchema = require('./fastify-schema/dotEnvSchema');

// dot env option written

const dotenvOpts = {
    schema: dotEnvSchema,
    dotenv: {
        path: `${__dirname}/../.env`,
        // debug: true
    }
}

async function launch(){

    // registering fastify plugin
    await fastify.register(require('fastify-env'), dotenvOpts);
    await fastify.register(require('fastify-jwt'), {
        secret: fastify.config.JWT
    });
    await fastify.register(require('fastify-formbody'));
    await fastify.register(require('./database/mongoDbConnector'));

    // registering custom decorator

    await fastify.register(jwtVerify);

    // registering custom user routes
    //await fastify.register(authRoute, {
  //      prefix: "/user"
   // });

    await fastify.register(questionRoute, {
        prefix: "/user"
    });

    fastify.listen(fastify.config.ENV === "development" ? '3000' : fastify.config.PORT, (err, addr) => {
        if(err){
            fastify.log.error(err);
            process.exit(1);
        }
        fastify.log.info(`server listenning on ${addr}`)

    })

}

launch()

jwtAuthVerify.js

const fastifyPlugin = require('fastify-plugin');

async function jwtAuthVerify(fastify, opts){

    fastify.decorate('jwtVerify', async(req, rep, next)=>{
        const accessToken = req.headers['x-auth-access-token'];
        try{
            const decoded = fastify.jwt.verify(accessToken);
            if(!decoded) return {status: "error", msg: "access token is invalid or not verified"};
            console.log(decoded);
            next();
        }catch(err){
            if(err){
                rep.send({status: "error", msg: err.message})
            }
        }

    })

}

module.exports = fastifyPlugin(jwtAuthVerify);

question.js routes

async function question(fastify, opts){

    const userQuestionDb = fastify.mongo.db.collection('userQuestions');

    fastify.post('/question', {preValidation: [fastify.jwtVerify]}, async(req, rep)=>{

        const {postType, postTitle, postContent} = req.body;
        const xAuthAccessToken = await req.headers['x-auth-access-token'];
        const decoded = await fastify.jwt.decode(xAuthAccessToken);
        const {_uId, role} = decoded;

        switch(postType){
            case '[text]':
                const postData = {
                    _uId,
                    postTitle,
                    postDate: Date(),
                    postUpdateDate: null,
                    postContent: null,
                    postType,
                }

                const questionAdded = await userQuestionDb.insertOne(postData);
                if(!questionAdded) return {status: "error", msg: "post does not added"};
                const {insertedId} = questionAdded;
                rep.send({insertedId});

            break;
            case '[text,images]': 

            break;
            default:
                console.log("defaults");

                break;

        }

    })

    fastify.get('/questions', async(req, rep)=>{

    })

}

module.exports = question;
mcollina commented 3 years ago

You are mixing callbacks with async in jwtVerify. Remove next() and you'd be good.

mcollina commented 3 years ago

Prefer returning an object to using reply.send(). It's simpler and less error prone. Moroever, always return an object in your handler.

Adityapanther commented 3 years ago

@mcollina as your response we don't have to use next() callback in preValidation like express middlewere

mcollina commented 3 years ago

You either use promises or callbacks.

Adityapanther commented 3 years ago

ok thanks @mcollina

Adityapanther commented 3 years ago

ok thanks @mcollina