A
A
Arseniy Ilyashov2015-05-15 09:14:12
JavaScript
Arseniy Ilyashov, 2015-05-15 09:14:12

How to avoid errors occurring before all scripts are connected?

Good day.
To filter content by tags that are set by the user, I use the ngTagsInput module, filtering occurs using such code, which is located in app.js

app.filter('filterByTags', function () {
    return function (items, tags) {
        var filtered = [];
        (items || []).forEach(function (item) {
            var matches = tags.some(function (tag) {
                return (item.text.indexOf(tag.text) > -1) ||
                    (item.text.indexOf(tag.text) > -1);
            });
            if (matches) {
                filtered.push(item);
            }
        });
        if (tags === undefined || tags.length === 0) {
            return items;
        } else {
            return filtered;
        }
    };
});

This script is processed even before my controllers and services are connected, and because of this, errors fall out to the console, because. this script, for example, tries to get the length of arrays that are currently undefined.
TypeError: Cannot read property 'length' of undefined

These errors do not interfere with the operation of the application as a whole, but personally they annoy me very much, and it seems to me that this should not happen when everything is done normally. How can you get rid of these errors?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shinkar, 2015-05-15
@Tavion

I see 3 options, but I don’t guarantee that at least 1 of them will work as it should))) but they will still push you to the right thoughts ...
1) Place the code somewhere in the footer
2) Set the if "condition when fully loaded page"
3) Register ignoring errors (probably the worst option)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question