Puzzle84 Puzzle84 - 6 days ago 5
TypeScript Question

Why does my function not return the error string?

I have function below. It does what it needs to except that it does not return the error string I want.

It always returns "".

I've put breakpoints and seen it step into each error case, but it doesn't return there. It returns at the end of the function.

I'm lost, I'm sure I'm making a really stupid mistake but I don't get it...

Save me the few hairs I have please :)

public validatePanel = () => {
this.Queries().forEach(function(q, i) {

if(q.from() == "" || q.from() == null || q.from() == undefined) {
return "Please select a database";
}

if(q.select().length > 0) {
q.select().forEach(function(s, j) {
if(s.selectoption() == "" || s.selectoption() == null || s.selectoption() == undefined){
return "Please select a stat to show";
}
});
}

if(q.where().length > 0) {
q.where().forEach(function(w, j) {
if(w.whereoption() == "" || w.whereoption() == null || w.whereoption() == undefined){
return "Please select a filter to filter on";
}

if(w.wherevalue() == "" || w.wherevalue() == null || w.wherevalue() == undefined) {
return "Please select a value for your filter";
}
});
}
});

return "";
}

gh9 gh9
Answer

As pointed out by Alex Bykov, your forEach function is not causing a return.

Your question on why not, per the MDN

The return value of the function is undefined

Return

value undefined.

Which means nothing you can do will generate a return value you can use. Also per the MDN there is no way to stop or break the loop other than throwing an exception.

There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool, use a plain loop instead. If you are testing the array elements for a predicate and need a Boolean return value, you can use every() or some() instead. If available, the new methods find() or findIndex() can be used for early termination upon true predicates as well.

Which means you will need to throw your exception in the forEach loop and then catch the exception and return the string like below (unless you use a normal for loop then you can do whatever you please)

try {
       this.Queries().forEach(function(q, i) {

            if(q.from() == "" || q.from() == null || q.from() == undefined) {
                throw "Please select a database";
            }

            if(q.select().length > 0) {
                q.select().forEach(function(s, j) {
                    if(s.selectoption() == "" || s.selectoption() == null || s.selectoption() == undefined){
                        throw "Please select a stat to show";
                    }
                });
            }

            if(q.where().length > 0) {
                q.where().forEach(function(w, j) {
                    if(w.whereoption() == "" || w.whereoption() == null || w.whereoption() == undefined){
                        throw "Please select a filter to filter on";
                    }

                    if(w.wherevalue() == "" || w.wherevalue() == null || w.wherevalue() == undefined) {
                        throw "Please select a value for your filter";
                    }
                });
            }
        });
}
catch(err) {
console.log(error);
}