Return javascript function within another function

Asked

Viewed 199 times

-1

I am making a call via ajax and returning an array. Using the.log console I see the result of the query. But what I need to call this result inside another google Chart function

quantityDaySurgeries = function(){
  var array = '';
  $.ajax({
    async: false,
    type: "POST",
    dataType: "json",
    url:  getUrl + 'informations/dashboard',
    data: {datec:$("#datepicker_3").val(),dater:$("#datepicker_4").val() },
    success: function(data) {
      $.each( data.daysurgerys, function(index,element){
        array += '["'+element.day+'",'+element.quantity+','+element.pen+','+element.nop+'],';
      })     
    }
  });

  return array;
};

If I call her that it doesn’t come dice

function drawVisualization() {
  var data = google.visualization.arrayToDataTable([
    ['Month', '','',''],
    quantityDaySurgeries()
  ]);

Could someone help me? Thank you

1 answer

1

The $.ajax will not execute the success before your return, you can work with Promise or with callback functions to get the expected result.

Example: Promise

quantityDaySurgeries = function () {
    return new Promise((res, rej) => {
        var array = '';
        $.ajax({
            async: false,
            type: "POST",
            dataType: "json",
            url: getUrl + 'informations/dashboard',
            data: { datec: $("#datepicker_3").val(), dater: $("#datepicker_4").val() },
            success: function (data) {

                $.each(data.daysurgerys, function (index, element) {
                    array += '["' + element.day + '",' + element.quantity + ',' + element.pen + ',' + element.nop + ']';

                })

                res(array);
            },
            error: function (err) {
                rej(err);
            }
        });
    })
};

// utilização

function drawVisualization() {
    quantityDaySurgeries().then(function (result) {
        var data = google.visualization.arrayToDataTable([
            ['Month', '', '', ''],
            result
        ]);

        // o resto do seu código
    }).catch(function(err) {
        // deu erro..
    });
}

Example: Callback function

quantityDaySurgeries = function (onSuccess, onError) {
        var array = '';
    $.ajax({
        async: false,
        type: "POST",
        dataType: "json",
        url: getUrl + 'informations/dashboard',
        data: { datec: $("#datepicker_3").val(), dater: $("#datepicker_4").val() },
        success: function (data) {

            $.each(data.daysurgerys, function (index, element) {
                array += '["' + element.day + '",' + element.quantity + ',' + element.pen + ',' + element.nop + ']';

            })

            onSuccess(array);
        },
        error: function (err) {
            onError(err);
        }
    });
};

// utilização
function drawVisualization() {
    quantityDaySurgeries(function(result) {
        var data = google.visualization.arrayToDataTable([
            ['Month', '', '', ''],
            result
        ]);

        // o resto do seu código
    }, function (err) {
        // erro...
    })
}
  • I used your example of Promise but the return does not appear inside the Chart function, only if you give the console.log that I see the result.

  • @marcelo_faria do not know how Chart works, but theoretically the process to get and work the data is this. If you run with the input data manually in the function, it works?

  • I was pulling from the database and iterating php array right into it and it works normally. Ali vai ficar resultados assim ["11/11",0,0,0],["12/11",0,0,0],["13/11",3,2,1],["14/11",0,0,0],["15/11",0,0,0] .... but how I need to later make queries of different dates , I’m using javascript to not need to refresh the page for different searches and such

  • @marcelo_faria updated the answer. Forehead again please.

  • The way that Voce did now is returning an array for each item, it will not work like this, because it only needs to return the items separated by commas and within the brackets ["11/11",0,0],["12/11",0,0].... the previous code it returns the way it needs, only it is not displaying the data just calling the RESULT, if it calls console.log(result) it shows the data coming straight, but inside the function it does not return anything

  • the problem is only being in displaying the return, the data is coming right, but it does not display inside the other function by calling it just as RESULT

  • @marcelo_faria where you put the console.log?

  • I put where RESULT is, then it displays the correct data, but if you take the console and leave only the RESULT does not appear the information to generate the chart

Show 3 more comments

Browser other questions tagged

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