简体   繁体   English

为什么我的函数不返回错误字符串?

[英]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 "";
    }

As pointed out by Alex Bykov, your forEach function is not causing a return. 正如Alex Bykov指出的那样,您的forEach函数不会引起返回。

Your question on why not, per the MDN 关于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. 同样,根据MDN,除了引发异常之外,没有其他方法可以停止或中断循环。

There is no way to stop or break a forEach() loop other than by throwing an exception. 除了抛出异常外,没有其他方法可以停止或中断forEach()循环。 If you need such behavior, the forEach() method is the wrong tool, use a plain loop instead. 如果您需要这种行为,则forEach()方法是错误的工具,请改用普通循环。 If you are testing the array elements for a predicate and need a Boolean return value, you can use every() or some() instead. 如果要为谓词测试数组元素并且需要布尔返回值,则可以改用every()或some()。 If available, the new methods find() or findIndex() can be used for early termination upon true predicates as well. 如果可用,新方法find()或findIndex()还可用于在真谓词上提前终止。

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 ) 这意味着您将需要在forEach循环中引发异常,然后捕获该异常并返回如下所示的字符串( 除非您使用常规的for循环,否则您可以执行任何操作

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM