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.

3
  • 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. Commented May 4, 2021 at 5:03
  • @Yousaf thanks for comments. but i tried without promise and same result comes. Commented May 4, 2021 at 5:17
  • See if this question helps you debug your problem. Commented May 4, 2021 at 5:19

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

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.
return res.json(set_permission_filter_array); showing following error. (node:2092) UnhandledPromiseRejectionWarning: ReferenceError: set_permission_filter_array is not defined
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.