Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to return properly using async in this code

So i have this code

const log = require("./queries/logQuery");
const moment = require('moment');
const morgan = require('morgan');

const custom = async (tokens, req, res) => {
    if(req.session){
        if(req.session.superadmin){
            const date = moment().locale('id').format('DD-MM-YYYY HH:mm:ss');
            const usr = req.session.superadmin;
            const method = tokens.method(req, res);
            const endpoint = tokens.url(req, res);
            const statusCode = tokens.status(req, res);

            await log.addLog(date, usr, method, endpoint, statusCode)
        }else if(req.session.user){
            const date = moment().locale('id').format('DD-MM-YYYY HH:mm:ss');
            const usr = req.session.user;
            const method = tokens.method(req, res);
            const endpoint = tokens.url(req, res);
            const statusCode = tokens.status(req, res);
    
            await log.addLog(date, usr, method, endpoint, statusCode)
        }
        return [
            tokens.method(req, res),
            tokens.url(req, res),
            tokens.status(req, res),
            tokens.res(req, res, 'content-length'), '-',
            tokens['response-time'](req, res), 'ms'
        ].join(' ')
    }
}

router.use(morgan(custom))

I’m trying to use async in the morgan logger, however the return always gave me "[object Promise]", i’ve tried setting each of the return using await but to no avail, how to fix this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is a confusing question. custom is an async function thus is ALWAYS returns a promise. That’s how ALL async functions work.

But, it looks like morgan wants the callback you pass it to synchronously return that array. You can’t do what you’re trying to do the way you’re trying to do it as the morgan API does not support using an async callback.

One way to solve this problem would be to remove the await on await log.addLog(...) so you can then remove the async on the custom function:

const custom = (tokens, req, res) => {
    if(req.session){
        if(req.session.superadmin){
            const date = moment().locale('id').format('DD-MM-YYYY HH:mm:ss');
            const usr = req.session.superadmin;
            const method = tokens.method(req, res);
            const endpoint = tokens.url(req, res);
            const statusCode = tokens.status(req, res);

            log.addLog(date, usr, method, endpoint, statusCode).catch(err => console.error);
        }else if(req.session.user){
            const date = moment().locale('id').format('DD-MM-YYYY HH:mm:ss');
            const usr = req.session.user;
            const method = tokens.method(req, res);
            const endpoint = tokens.url(req, res);
            const statusCode = tokens.status(req, res);
    
            log.addLog(date, usr, method, endpoint, statusCode).catch(err => console.error);
        }
        return [
            tokens.method(req, res),
            tokens.url(req, res),
            tokens.status(req, res),
            tokens.res(req, res, 'content-length'), '-',
            tokens['response-time'](req, res), 'ms'
        ].join(' ')
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading