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

时间:2022-09-21 15:46:17

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

1 个解决方案

#1


2  

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

除了抛出异常之外,没有办法停止或中断forEach()循环。如果你需要这样的行为,forEach()方法是错误的工具,而是使用普通循环。如果要测试谓词的数组元素并需要布尔返回值,则可以使用every()或some()。如果可用,新方法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);
}

#1


2  

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

除了抛出异常之外,没有办法停止或中断forEach()循环。如果你需要这样的行为,forEach()方法是错误的工具,而是使用普通循环。如果要测试谓词的数组元素并需要布尔返回值,则可以使用every()或some()。如果可用,新方法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);
}