Filter function returns Undefined - Jquery

Asked

Viewed 45 times

0

Hello!

Can someone tell me what’s wrong ?

Using the filter function, I am seeing a Undefined return.

Javascript

var enum_toastr_type = {
    success: 1,
    info: 2,
    warning: 3,
    error: 4
}

/***# PAGELOAD START #***/
$(document).ready(function() {
    toastr.options = {
        progressBar: true,
        closeButton: false,
        positionClass: 'toast-bottom-right',
        timeOut: '20000'
    }
    /*
	var visited = localStorage.getItem('visited');
    if(!visited) {
    setTimeout('get_mensagem_ajax()', 50);
	 localStorage.setItem('visited', true);
    }
	*/
    setTimeout('get_mensagem_ajax()', 50);
});
/***# PAGELOAD END #***/


/*** Métodos Início ***/
function exibe_mensagem(mensagens) {
    var msg = mensagens.filter(function(x) {
        return x;
    });
    exibe_mensagem_toastr(msg);
}

function exibe_mensagem_toastr(mensagens) {
    $(mensagens).each(function() {
        //console.log(this.mensagem);
        switch (this.tipo_notificacao) {
            case enum_toastr_type.info:
                toastr.info(this.mensagem);
                break;
            case enum_toastr_type.success:
                toastr.success(this.mensagem);
                break;
            case enum_toastr_type.warning:
                toastr.warning(this.mensagem);
                break;
            case enum_toastr_type.error:
                toastr.error(this.mensagem);
                break;
        }
    });
}
/*** Métodos Fim ***/

/***# AJAX START #***/
function get_mensagem_ajax() {
    $.ajax({
        type: 'GET',
        contentType: 'application/json; charset=utf-8',
        url: 'notificacao/get_mensagem',
        success: (function(data) {
            exibe_mensagem(data);
        }),
        error: (function(erro) {
            trata_erro_ajax(erro);
        })
    });
}
/*** AJAX END ***/
inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • tries so $(messages). filter

  • It didn’t work either

  • Have you seen if $(messages) or messages are returning anything? It may no longer be found. if you can, post your html too

  • Both are returning and displaying the array as per image, but when will I check the this, in function exibe_mensagem_toastr have Undefined.

  • got it, so let’s try another try: $(messages). each(Function(i, item) {.... and then try to access: item.type_notification

  • Try adding the index [1] in the variable: $(mensagens[1]).each(function() {

  • Yes, with the index I can, but I found the problem,the problem is in Enum, which expected the type INT, but it was coming with string, fixed the return on the server and worked smoothly. Thank you very much

Show 2 more comments
No answers

Browser other questions tagged

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