1

when executing the following code firebug tells me: msj[0] is undefined what is the problem?

What I want to do is that every time it receives a response after every run, catch it in the array after calling msg to generate a single alert to show me all the answers together...

I don't understand why me show undefined if the array has data, in this case has three data..

var msj = [];

for (var a = document.querySelectorAll('table.inventory tbody tr'), i = 0; a[i]; ++i) {
    // get inventory row cells
    cells = a[i].querySelectorAll('span:last-child');
    // buscar la selecion del concepto
    var opciones = cells[0].querySelectorAll('option:checked');
    var value_concept = opciones[0].value;
    // set price as cell[2] * cell[3]
    var Uno = value_concept;
    var Dos = cells[1].innerHTML ;
    var Tres = parseFloatHTML(cells[2]);
    var Cuatro = parseFloatHTML(cells[3]);
    var Cinco = parseFloatHTML(cells[4]);

    var id_fac = id_fac_select;
    var valor_fac = costo_fac_select.split(".");
    var valor_fact = valor_fac[0];
    var valor_recibo = Cinco;

    $.ajax({
        url:"js.php",
        cache:false,
        type:"POST",
        data:{concepto:Uno,descripcion:Dos,valor_total:Tres,valor_pagado:Cuatro,valor_f:valor_fact,saldo_pendiente:valor_recibo,numero_factura:id_fac,id_estudiante:id_student},
        success:function(result)
        {   
            msj.push(result);               
        }
        });

}
console.log(msj);
alert(msj[0]);
Al3mor
  • 35
  • 7

2 Answers2

2

You are seeing undefined in the alert() because msj doesn't have data yet. Your code that populates msj is not called until the browser receives a response from the server, but your alert(msg[0]) code is called immediately.

If you are seeing data in the console from your console.log(msj), this is because the console doesn't evaluate objects until you expand them. See this question. Try changing your console.log to something like one of these:

console.log(JSON.stringify(msj));
console.log(msj.length);
console.log(msj.join(","));

Edit: It sounds like you want to gather all of the responses, and then do something once you've received all of them. In your success handler, after you have added the result to msj, check the length to see if you've received all of them. When all of the responses have been received, msj.length should be the same as a.length:

success: function (result) {   
    msj.push(result);
    if (msj.length == a.length) {
        // All responses have been received. Ready to use msj!
    }
}
Community
  • 1
  • 1
gilly3
  • 87,962
  • 25
  • 144
  • 176
  • thank you for answers, but did not work... when change async in false yes work, but it is a bad practice... i don't know as i do.... help me please. – Al3mor Apr 02 '14 at 22:49
  • @Al3mor, try moving the `console` and `alert` inside the `success` function. That way they will not be called until there's something in `msj`. – Jonathan M Apr 02 '14 at 22:51
  • of this mode works, but generate message each time through the sentence for... what I need is to generate all the answers in a single alert and thought I could do this by storing the data in an array, but does not work... you know if i can do it otherwise? – Al3mor Apr 02 '14 at 22:57
  • @gilly3 Is running correctly... thank you very much... // No te imaginas lo agradecida que estoy... Muchisimas Gracias por toda tu ayuda.... un abrazo desde Colombia! – Al3mor Apr 03 '14 at 16:14
0

Move the console and alert calls into the success function.

$.ajax({
    url:"js.php",
    cache:false,
    type:"POST",
    data:{concepto:Uno,descripcion:Dos,valor_total:Tres,valor_pagado:Cuatro,valor_f:valor_fact,saldo_pendiente:valor_recibo,numero_factura:id_fac,id_estudiante:id_student},
    success:function(result)
    {   
        msj.push(result);               
        console.log(msj);
        alert(msj[0]);        }
    });
Jonathan M
  • 17,145
  • 9
  • 58
  • 91
  • of this mode works, but generate message each time through the sentence for... what I need is to generate all the answers in a single alert and thought I could do this by storing the data in an array, but does not work... you know if i can do it otherwise? – Al3mor Apr 02 '14 at 22:55