TypeError: answer [0] is undefined [duplicated]

0

How are you?

I have this Code:

$(".validarIdProducto").change(function(){

	$(".alert").remove();

	var idProducto = $(this).val();
	// console.log("​idProducto", idProducto)

	var datos = new FormData();
	datos.append("validarIdProducto", idProducto);

		$.ajax({
		url:app+"ajax/archivoencuestion.php",
		method:"POST",
		data: datos,
		cache: false,
		contentType: false,
		processData: false,
		dataType: "json",
		success:function(respuesta){

			if(respuesta[0]["id"].length != 0 && respuesta[0]["id_administrador"] != id_tienda){
				$(".validarIdProducto").parent().parent().after('<div class="alert alert-danger"><i class="mdi mdi-thumb-down-outline mr-1"></i>Esa publicación no es tuya sino de otro vendedor. Revisa el ID.</div>');
				$(".validarIdProducto").val("");

			}else if(respuesta[0]["id"].length != 0 && respuesta[0]["id_administrador"] == id_tienda){
				/* entonces está bien... */

			}

		}

	})

})

In other words, what I do is bring an array from the database. It goes from 10, it brings it and if it finds a match (if that ID is found in the database) but it is from another vendor, I get an alert that says it belongs to another vendor. If that ID exists but the publication is yours and not another, it does not execute anything (else if) but if that ID does not even exist, it comes out:

  

In Firefox Developer Edition: "TypeError: answer [0] is undefined".

     

In Chrome: "Uncaught TypeError: Can not read property 'id' of   undefined "

The answer I get is: array(0) { }

How can I do with an else if I recognize that array(0) { } to execute a statement? I mean, how do I express it?

    
asked by Franco Nicolas Vega 26.12.2018 в 08:16
source

1 answer

0

You must check if there is a response [0]. I have modified your code adding a possibility to do this verification, but there are many possibilities to do it.

$(".validarIdProducto").change(function(){

    $(".alert").remove();

    var idProducto = $(this).val();
    // console.log("​idProducto", idProducto)

    var datos = new FormData();
    datos.append("validarIdProducto", idProducto);

    $.ajax({
        url:app+"ajax/archivoencuestion.php",
        method:"POST",
        data: datos,
        cache: false,
        contentType: false,
        processData: false,
        dataType: "json",
        success:function(respuesta){
            if( typeof respuesta[0] != "undefined" ){ //Comprobando si existe respuesta[0], si existe hacemos esto....
                if(respuesta[0]["id"].length != 0 
                   && respuesta[0]["id_administrador"] != id_tienda){

                    $(".validarIdProducto").parent().parent().after(
                    '<div class="alert alert-danger">
                     <i class="mdi mdi-thumb-down-outline mr-1">
                     </i>Esa publicación no es tuya sino de otro vendedor. Revisa el 
                     ID. 
                     </div>');
                    $(".validarIdProducto").val("");

                }else if(respuesta[0]["id"].length != 0 &&
                     respuesta[0]["id_administrador"] == id_tienda){
                /* entonces está bien... */
                }

           }else{
                /* NO EXISTE EL ID */
           }

        }

   })

})
    
answered by 26.12.2018 в 10:10