Gab*_*vay 10 javascript mongodb
写作:
if (...) {
return;
}
Run Code Online (Sandbox Code Playgroud)
在MongoDb shell脚本中会抱怨: SyntaxError: return not in function
我也尝试过exitmongo console命令:
if (...) {
exit;
}
Run Code Online (Sandbox Code Playgroud)
但是你得到错误: ReferenceError: exit is not defined
如何才能提前终止js脚本文件中的执行?
我使用以下解决方案:
(function(){
// now you can use the return keyword anywhere:
if (/* some condition */) {
print("This is an error condition");
return;
}
})();
Run Code Online (Sandbox Code Playgroud)
我知道以这种方式终止脚本不会使mongo返回的错误代码不同于0(就像接受的解决方案一样)。