How to resolve error "Can not read property 'substring' of null"

2

I need to display data from a DB in a filter area but I'm not getting it, it only works if I use one of the filters, and in the console it has the following error "Can not read property 'substring' of null".

Controller code:

  .filter('daterangeentrevistado', function () {
        return function (conversations, start_date, end_date) {
            var result = [];

        var start_date = start_date ? new Date(start_date.substring(start_date.length - 4, start_date.length) + "/" + start_date.substring(start_date.length - 7, start_date.length - 5) + "/" + start_date.substring(0, start_date.length == 10 ? 2 : 1)).getTime() : 0;
        var end_date = end_date ? new Date(end_date.substring(end_date.length - 4, end_date.length) + "/" + end_date.substring(end_date.length - 7, end_date.length - 5) + "/" + end_date.substring(0, end_date.length == 10 ? 2 : 1)).getTime() : new Date().getTime();


        if (conversations && conversations.length > 0) {
            $.each(conversations, function (index, conversation) {
                var conversationDate = new Date(conversation.datanascimento.substring(conversation.datanascimento.length - 4, conversation.datanascimento.length) + "/" + conversation.datanascimento.substring(conversation.datanascimento.length - 7, conversation.datanascimento.length - 5) + "/" + conversation.datanascimento.substring(0, conversation.datanascimento.length == 10 ? 2 : 1)).getTime();

                if (conversationDate >= start_date && conversationDate <= end_date) {

                    result.push(conversation);
                }
            });

            return result;
        }
    };
});
    
asked by anonymous 19.02.2018 / 20:36

1 answer

2

First, I recommend you rename the variables start_date and end_date so as not to create any conflicts with the parameters passed to the return function. To make things easier, let's rename the parameters:

return function (conversations, start_date_param, end_date_param) {.....

Then, we check in conversation.datanascimento just as you do in other variables as well. Let's create a variable for datanascimento to make the substring more readable:

var dataNascimento = conversation.datanascimento;

And then, we check it out:

if(dataNascimento) {
    var conversationDate = new Date(dataNascimento.substring(dataNascimento.length - 4, dataNascimento.length) + "/" + dataNascimento.substring(dataNascimento.length - 7, dataNascimento.length - 5) + "/" + dataNascimento.substring(0, dataNascimento.length == 10 ? 2 : 1)).getTime();
}

Here is your code with all the modifications I've listed:

.filter('daterangeentrevistado', function () {

    return function (conversations, start_date_param, end_date_param) {

        var result = [];

        var start_date = start_date_param ? new Date(start_date_param.substring(start_date_param.length - 4, start_date_param.length) + "/" + start_date_param.substring(start_date_param.length - 7, start_date_param.length - 5) + "/" + start_date_param.substring(0, start_date_param.length == 10 ? 2 : 1)).getTime() : 0;
        var end_date = end_date_param ? new Date(end_date_param.substring(end_date_param.length - 4, end_date_param.length) + "/" + end_date_param.substring(end_date_param.length - 7, end_date_param.length - 5) + "/" + end_date_param.substring(0, end_date_param.length == 10 ? 2 : 1)).getTime() : new Date().getTime();

        if (conversations && conversations.length > 0) {
            $.each(conversations, function (index, conversation) {

                var dataNascimento = conversation.datanascimento;

                if(dataNascimento) {

                   var conversationDate = new Date(dataNascimento.substring(dataNascimento.length - 4, dataNascimento.length) + "/" + dataNascimento.substring(dataNascimento.length - 7, dataNascimento.length - 5) + "/" + dataNascimento.substring(0, dataNascimento.length == 10 ? 2 : 1)).getTime();

                    if (conversationDate >= start_date && conversationDate <= end_date) {
                        result.push(conversation);
                    }

                }
                else {
                    alert("Aparentemente conversation.datanascimento (dataNascimento) é nulo: " + dataNascimento);
                }

            });

            return result;
        }

    };

});
    
20.02.2018 / 18:57