Custard
Custard

Reputation: 49

NodeJs Javascript Calling a Function Dynamically From an Array

So I've searched around stack overflow and cant seem to find anything that people have suggested that works.

So I have an Object Array

Report_Search_List = [
    {"NAME":"CHART OF ACCOUNTS", "PDF":"CHART_OF_ACCOUNTS_PDF", "XLS":"CHART_OF_ACCOUNTS_XLS"},
    {"NAME":"GENERAL LEDGER", "PDF":"GENERAL_LEDGER_PDF", "XLS":"GENERAL_LEDGER_XLS"},
    {"NAME":"COST REPORT", "PDF":"COST_REPORT_PDF", "XLS":"COST_REPORT_XLS"},
    {"NAME":"CASH FLOW", "PDF":"CASH_FLOW_PDF", "XLS":"CASH_FLOW_XLS"},
    {"NAME":"INVOICE", "PDF":"INVOICE_PDF", "XLS":"INVOICE_XLS"},
    {"NAME":"CREDIT NOTE", "PDF":"CREDIT_NOTE_PDF", "XLS":"CREDIT_NOTE_XLS"},
    {"NAME":"JOBCARD COST", "PDF":"JOBCARD_COST_PDF", "XLS":"JOBCARD_COST_XLS"},
    {"NAME":"GOODS RECEIVED VOUCHER", "PDF":"GOODS_RECEIVED_VOUCHER_PDF", "XLS":"GOODS RECEIVED VOUCHER_XLS"},
    {"NAME":"GOODS RETURNED NOTE", "PDF":"GOODS_RETURNED_NOTE_PDF", "XLS":"GOODS_RETURNED_NOTE_XLS"},
    {"NAME":"REQUISITION", "PDF":"REQUISITION_PDF", "XLS":"REQUISITION_XLS"},
    {"NAME":"DELIVERY NOTE", "PDF":"DELIVERY_NOTE_PDF", "XLS":"DELIVERY_NOTE_XLS"},
    {"NAME":"PICK SLIP", "PDF":"PICK_SLIP_PDF", "XLS":"PICK_SLIP_XLS"},
    {"NAME":"PETTY CASH", "PDF":"PETTY_CASH_PDF", "XLS":"DELIVERY_NOTE_XLS"},
    {"NAME":"OTHER TRANSACTIONS", "PDF":"OTHER_TRANSACTIONS_PDF", "XLS":"OTHER_TRANSACTIONS_XLS"},
    {"NAME":"PURCHASE ORDER", "PDF":"PURCHASE_ORDER_PDF", "XLS":"PURCHASE_ORDER_XLS"}
]

So it first builds a Search list for the user to select from

Report_Search_List[index].NAME

Then once they have selected the report they click on the button either PDF or XLS

here is the function

function PDF_CLICK() {
    try {
        var text = "none";
        var BRANCH_PDF_REPORTS_PROFILE_SELECT = document.getElementById('BRANCH_PDF_REPORTS_PROFILE_SELECT');
        text = BRANCH_PDF_REPORTS_PROFILE_SELECT.value;
        //get array list
        var report = Report_Search_List.filter(function (el) {
            return el["NAME"] == text;
        });
        //PROBLEM IS HERE
//run method
Window[report[0]["PDF"]]();
window[report[0]["PDF"]]();
report[0]["PDF"].call();
report[0]["PDF"]();
    } catch (e) {
        console.log(e);
        EX_JS_ALERT.ALERT("OOPS SOMETHING WENT WRONG"); EX_JS_ERROR.ERROR(e.toString(), location.pathname);;
    }
}

Now the problem

I've tried all of the above and it is getting the correct report but still comes back with its not a function. Meanwhile there is a function below.

Upvotes: 1

Views: 1057

Answers (2)

Custard
Custard

Reputation: 49

So I figured out how to do it window is not the current window you are working on.

so what i did at the top of the page is

const EX_JS_BRANCH_REPORTS_PDF = require('../BRANCH/EX_JS_BRANCH_REPORTS_PDF.js');

then changed the

function to

exports.GENERAL_LEDGER_PDF = function(){}

then I changed window to EX_JS_BRANCH_REPORTS_PDF and dynamically working XD

EX_JS_BRANCH_REPORTS_PDF[report[0]["PDF"]]();

Upvotes: 0

Mark
Mark

Reputation: 92440

This is tagged node.js so I assume this isn't running in a browser. As such, there is not windowobject to use to find global variables. You can explicitly define things on the global object in Node, but that's rarely a good idea. A better way is to create an object that holds your functions with keys that are named with the text you want to use to call them.

So with a subset of you data you might have an arrangement like:

const Report_Search_List = [
    {"NAME":"CHART OF ACCOUNTS", "PDF":"CHART_OF_ACCOUNTS_PDF", "XLS":"CHART_OF_ACCOUNTS_XLS"},
    {"NAME":"GENERAL LEDGER", "PDF":"GENERAL_LEDGER_PDF", "XLS":"GENERAL_LEDGER_XLS"}
]
    
const functions = {
  CHART_OF_ACCOUNTS_PDF(){console.log("chart called")},
  GENERAL_LEDGER_PDF(){console.log("Ledger call")}
  
}

// now you can call them with strings:
functions[Report_Search_List[0]["PDF"]]()
functions[Report_Search_List[1]["PDF"]]()

Upvotes: 1

Related Questions