returns data with module.Exports Node js

Asked

Viewed 490 times

0

I’m having trouble returning data using module.Xports, here’s an example of how I’m trying to do it

module.exports.menu = (app,req,res)=>{ 
    var fs = require('fs');
    var patch = 'public/menu';
    var conn = app.config.dbSyncSql();


    fs.readdir(patch, (err, files) => {
        /**
         * Aqui executamos uma consulta SQL e verificamos a tabela do login qual e o idioma
         */
        var linhas_login = conn.queueQuery(`SELECT * FROM login WHERE id = '1'`)();
        var lang = linhas_login[0].lang;

        /**
         * Criamos um loop com os arquivos emcontrados no parametro fs.readdir 
         * abrimos cada arquivo separadamente e colocamos o seu conteudo em um objeto
         */
        function reload() {
            var obj = [];
            files.forEach(file => {

                var results = JSON.parse(fs.readFileSync(patch + '/' + file, 'utf8'));
                // console.log(results.lang);
                // console.log(lang);

                /**
                 * Aqui fazemos a verificação da liguagem e mostramos o menu de acordo com o idioma do cliente
                 */
                if (results.lang == lang) {
                    results.sub = results.menu.menu_sub;
                    obj.push(results);
                }
            });

            retorno(obj)
            //return obj;
        }
        //console.log(reload());
        return reload();

    });

    function retorno(x) {
        return x;
    }

}

but I can’t get the data

module.exports = (app)=>{

    app.get('/get_menu', (req, res) => {
        var conn = app.config.dbSyncSql();
        var mem = app.app.controlles.menu.menu(app,req,res);

        res.send(mem)

        //res.render('index', { menu: mem });

    });

}

the whole project is in this link https://github.com/AlexandreSousa/erpbilling


you can take a look at my folder structure https://github.com/AlexandreSousa/erpbilling/tree/master/app at this link then when I change this part of the code

function retorno(x) {
  st =  x;
}
return st;

it even works but whenever and the first time it accesses it from error

1 answer

0

I managed to solve with the help of a friend and the code I stay that way

/**
 * Autor: Guilherme Kaercher
 * @param {*} app 
 * @param {*} req 
 * @param {*} res 
 */

module.exports.menu = (app, req, res) => {
    var fs = require('fs');
    var path = 'public/menu/';
    var conn = app.config.dbSyncSql();

    // Aqui executamos uma consulta SQL e verificamos a tabela do login qual e o idioma

    var linhas_login = conn.queueQuery(`SELECT * FROM login WHERE id = '${req.session.login}'`)();
    var lang = linhas_login[0].lang;
    //const lang = 'pt-br';

    console.log('lang : ' + lang);

    var files = fs.readdirSync(path);
    console.log('files: ' + files);

    const obj = [];
    files.forEach(file => {

        console.log('obj: ' + obj);
        var results = JSON.parse(fs.readFileSync(path + file, 'utf8'));
        console.log(results);

        //Aqui fazemos a verificação da liguagem e mostramos o menu de acordo com o idioma do cliente
        if (results.lang == lang) {
            results.sub = results.menu.menu_sub;
            obj.push(results);
        }
    });
    return obj;
}
  • What has changed from one code to another? It would be interesting to explain what you did to fix.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.