0

sessionFilter.js

'use strict';
const knex = require('../config/database')

class SessionFilter{
    async setPermissionFilterArray(data){
        return new Promise(async (resolve, reject) => {
            try { 
                const sql_data = await knex.select(knex.raw(sql));
                var all_permission = []; 
                sql_data.forEach(function(val) {
                    if (all_permission[val.event_ref] === undefined) all_permission[val.event_ref] = [];
                    if (all_permission[val.event_ref][val.event_slug] === undefined) all_permission[val.event_ref][val.event_slug] = [];
                    all_permission[val.event_ref][val.event_slug][val.event_slug_key] = {
                        permission:val.permission,
                        no_permission:val.no_permission,
                        sql_where_clause:val.sql_where_clause
                    }
                });
                resolve(all_permission);
            } catch (error) {
                reject(error.message);
            }
        })
    }
}   
module.exports = SessionFilter;

server.js

const filter = new SessionFilter();
const set_permission_filter_array = await filter.testFunction(data);
console.log(set_permission_filter_array);
return res.json(set_permission_filter_array);

when i console set_permission_filter_array variable showing data well but when i return set_permission_filter_array variable showing blank array at frontend. how can i get data? please help me.

  • `async` functions always return a promise, so you don't need to create one yourself using the Promise constructor. Also, the executor function (function passed to the Promise constructor) should NOT be an `async` function. – Yousaf May 04 '21 at 05:03
  • @Yousaf thanks for comments. but i tried without promise and same result comes. – Zilon Jakir May 04 '21 at 05:17
  • See if [this question](https://stackoverflow.com/questions/28994496/why-does-express-js-return-an-empty-array-when-using-res-send/48022515) helps you debug your problem. – Yousaf May 04 '21 at 05:19

1 Answers1

0

Your function will automaticaly return a promise because of the async keyword. This promise will be resolved at the return all_permission; statement.

class SessionFilter{
   async setPermissionFilterArray(data){        
       try { 
           //don't know were this sql var comes from
           const sql_data = await knex.select(knex.raw(sql));             
           var all_permission = []; 
           sql_data.forEach((val) => { 
               ... 
           });
           return all_permission;
       } catch (error) {
           throw error;
       }        
   }
}   

Then you can get the result of your promise using async await keywords like this :

const set_permission_filter_array = await setPermissionFilterArray(data);
Raphayol
  • 961
  • 8
  • 14
  • OP is already getting the result from the `setPermissionFilterArray` function. Yes, the Promise constructor should be removed but that's not the cause of the problem here. – Yousaf May 04 '21 at 05:31
  • return res.json(set_permission_filter_array); showing following error. (node:2092) UnhandledPromiseRejectionWarning: ReferenceError: set_permission_filter_array is not defined – Zilon Jakir May 04 '21 at 05:34
  • Still blank array return. but when i push a single value in all_permission EXAMPLE:- all_permission[val.enent_ref].push(val.event_slug) then data show perfectly. but multidimensional array return blank. – Zilon Jakir May 04 '21 at 05:46