0

I have array initially declared in global scope as an empty array then inside a function callback. I have to push response message elements to this array.

But I need to get this update array outside callback of function

 var array = []
 frappe.call({
        method: "frappe.client.get_list",
        args: {
            doctype: 'Attendance',

       filters: [['attendance_date', '>=', values.from_date],
                ['attendance_date', '<=', values.to_date]

            ]
        },
        "callback": function (response) {
            console.log(response.message)
            for (var i in response.message) {
                frappe.call({
                    method: "frappe.client.get_value",
                    args: {
                        doctype: 'Attendance',
                        fieldname: ['branch'],
                    filters: { 'name': ['=', response.message[i].name] }

                    },
                    "callback": function (data) {
                        frappe.call({
                            method: "frappe.client.get_value",
                            args: {
                                doctype: 'Medical Crew Salaries',
                                fieldname: ['total'],
                             filters: { 'cost_center': ['=',data.message.branch]}
                            },
                             "callback": function (r) {
                                    array.push({ branch: data.message.branch, total: r.message.total })

                            }
                        })
                    }
                })
            }
    }
    })
console.log(array.length)
var result=[]
array.reduce(function(res, value) {
if (!res[value.branch]) {
    res[value.branch] = { branch: data.message.branch, total: 0 };
    result.push(res[value.branch])
  }
  res[value.branch].total += r.message.total;
  return res;
}, {});
console.log(result)
frappe.model.with_doctype('Journal Entry', function() {
        var journal_entry = frappe.model.get_new_doc('Journal Entry');
        for(var c in result){
                var journalentry_account= frappe.model.add_child(journal_entry, 'accounts');
                    journalentry_account.debit_in_account_currency=result[c].total
                    journalentry_account.cost_center=result[c].branch
            
        }
                frappe.set_route('Form', 'Journal Entry', journal_entry.name);      
                        })
    


The reason why I need to do that is that I need to loop trough this array outside, the first for loop array.

  • The array variable is scoped inside your function, so you could either declare it outside of the function or pass it to another function, where you will do your looping logic. – tomerpacific Apr 25 '21 at 09:11
  • out side existing for loop because inside first for loop iteration run twice – Zeinab Mohammed Apr 25 '21 at 09:13
  • If the callback is not called synchronously, but later, then you should only expect the array to be populated during the callback execution: you cannot expect an array to have the values *now*, when the array will only get populated in the *future*. – trincot Apr 25 '21 at 09:14
  • @trincot no it is called synchronously – Zeinab Mohammed Apr 25 '21 at 10:02
  • So what is the code that is not doing what you want? Can you show the code where you do this outside the existing loop -- I don't really understand what you are trying to do. Best is to show the code that is failing. Is `console.log(array.length)` outputting the expected size? – trincot Apr 25 '21 at 10:36
  • array.length should be the array length after pushing response elements; I just need to loop through this array but outside the already existing for loop it causes wrong total values inside array.reduce .. code updated – Zeinab Mohammed Apr 25 '21 at 10:46
  • *"...should be the array length after..."*: yes, I understand that. My question is: does it currently output what it should output? Your updated code has 20 opening braces and 21 closing braces. Can you also take care to properly indent your code? – trincot Apr 25 '21 at 10:50
  • @trincot array.length outputs zero outside callback function.inside callback it works fine..alse code updated again thanks – Zeinab Mohammed Apr 25 '21 at 11:31
  • That is why I asked this earlier on: it proves that your callbacks execute *asynchronously*, and not synchronously as you stated. NB/ the code is not properly indented. Please update. – trincot Apr 25 '21 at 11:40
  • @Zeinab Mohammed, This is so common question, I saw many questions like that, A month ago I answer [similar question](https://stackoverflow.com/questions/66831144/object-is-not-returning-the-value-outside-of-function-but-returning-it-inside-th/66831928#66831928), You may find something helpful... – Nur Apr 25 '21 at 12:16
  • @trincot so what is the solution regarding that? – Zeinab Mohammed Apr 25 '21 at 13:09
  • That's a whole subject. Read the reference at the top. – trincot Apr 25 '21 at 13:25
  • @trincot thanks so much I have restructured my code so it works fine now I appreciate your responses – Zeinab Mohammed Apr 25 '21 at 14:08
  • @Nur Thanks I was great reference – Zeinab Mohammed Apr 25 '21 at 14:09

0 Answers0