How to resolve "Cannot read Property 'substring' of null" error

Asked

Viewed 1,749 times

2

I need to display data from a BD in a filter area but I’m not getting it, it only works if I use one of the filters, and on the console it shows the following error "Cannot 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;
        }
    };
});
  • I need to get everyone in this comic book to show up even if I don’t use a filter

  • I would recommend you rename the variables start_date and end_date to differentiate them from the parameters passed in the return function and prevent any kind of conflict. Then you can do a variable check conversationDate, apparently can also be property conversation.datanascimento.

  • How can I perform this check?? Thanks in advance!!!

1 answer

2


First, I recommend you rename the variables start_date and end_date not to create any conflicts with the parameters passed to the return function. To facilitate a little we will 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 the datanascimento to make the substring more readable:

var dataNascimento = conversation.datanascimento;

And then we check:

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’s your code with all the modifications I mentioned:

.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;
        }

    };

});
  • Caaaraaaaaa, thank you very much, it worked. It helped me a lot!!!

Browser other questions tagged

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