优化这段代码function checkValidity(message) { mini.showTips({ content: message, state: 'danger', showIcon: false, timeout: 1000 }); } if (queryText === '') { checkValidity('请填写查询方法'); return false; } if (resourceData.length === 0) { checkValidity('请选择数据资源表数据'); return false; } if (!relateWhole) { checkValidity('请检查关联查询字段行完整性'); return false; } if (!screenWhole) { checkValidity('请检查筛选条件行完整性'); return false; } if (revealHasValue === 0) { checkValidity('展示字段必填'); return false; }
时间: 2024-01-07 07:05:31 浏览: 53
可以考虑使用一个数组来存储所有需要校验的条件及其对应的错误信息,然后使用循环遍历数组进行校验,并统一调用 `checkValidity` 函数输出错误信息。
具体实现如下:
```javascript
const validations = [
{ condition: queryText === '', message: '请填写查询方法' },
{ condition: resourceData.length === 0, message: '请选择数据资源表数据' },
{ condition: !relateWhole, message: '请检查关联查询字段行完整性' },
{ condition: !screenWhole, message: '请检查筛选条件行完整性' },
{ condition: revealHasValue === 0, message: '展示字段必填' }
];
for (let i = 0; i < validations.length; i++) {
if (validations[i].condition) {
checkValidity(validations[i].message);
return false;
}
}
```
这样可以将校验逻辑简化并提高代码的可维护性。同时,如果后续需要增加或修改校验条件,只需修改 `validations` 数组即可。
相关问题
if (queryText === '') { mini.showTips({ content: '请填写查询方法', state: 'danger', showIcon: false, timeout: 1000 }); return false; } if (resourceData.length === 0) { mini.showTips({ content: '请选择数据资源表数据', state: 'danger', showIcon: false, timeout: 1000 }); return false; } if (!relateWhole) { mini.showTips({ content: '请检查关联查询字段行完整性', state: 'danger', showIcon: false, timeout: 1000 }); return false; } if (!screenWhole) { mini.showTips({ content: '请检查筛选条件行完整性', state: 'danger', showIcon: false, timeout: 1000 }); return false; } if (revealHasValue === 0) { mini.showTips({ content: '展示字段必填', state: 'danger', showIcon: false, timeout: 1000 }); return false; }优化下
可以将重复的代码段抽象成一个函数,提高代码的可读性和可维护性,例如:
```
function checkValidity(message, type) {
if (type.length === 0) {
mini.showTips({
content: message,
state: 'danger',
showIcon: false,
timeout: 1000
});
return false;
}
return true;
}
```
然后在原来的代码中调用该函数,例如:
```
if (!checkValidity('请填写查询方法', queryText)) {
return false;
}
if (!checkValidity('请选择数据资源表数据', resourceData)) {
return false;
}
if (!checkValidity('请检查关联查询字段行完整性', relateWhole)) {
return false;
}
if (!checkValidity('请检查筛选条件行完整性', screenWhole)) {
return false;
}
if (!checkValidity('展示字段必填', revealHasValue)) {
return false;
}
```
这样修改后,代码变得更加简洁和易于维护。
2023-06-08 21:13:49,372 [myid:] - INFO [main:QuorumPeerConfig@135] - Reading configuration from: /opt/module/apache-zookeeper-3.5.7/bin/../conf/zoo.cfg 2023-06-08 21:13:49,389 [myid:] - INFO [main:QuorumPeerConfig@387] - clientPortAddress is 0.0.0.0:2181 2023-06-08 21:13:49,389 [myid:] - INFO [main:QuorumPeerConfig@391] - secureClientPort is not set 2023-06-08 21:13:49,394 [myid:] - ERROR [main:QuorumPeerMain@89] - Invalid config, exiting abnormally org.apache.zookeeper.server.quorum.QuorumPeerConfig$ConfigException: Error processing /opt/module/apache-zookeeper-3.5.7/bin/../conf/zoo.cfg at org.apache.zookeeper.server.quorum.QuorumPeerConfig.parse(QuorumPeerConfig.java:156) at org.apache.zookeeper.server.quorum.QuorumPeerMain.initializeAndRun(QuorumPeerMain.java:113) at org.apache.zookeeper.server.quorum.QuorumPeerMain.main(QuorumPeerMain.java:82) Caused by: java.lang.IllegalArgumentException: myid file is missing at org.apache.zookeeper.server.quorum.QuorumPeerConfig.checkValidity(QuorumPeerConfig.java:736)
这段日志是Zookeeper启动时输出的,它显示了Zookeeper在读取配置文件时遇到了问题。具体来说,它指出了myid文件缺失,而这个文件是Zookeeper用来标识节点编号的文件。因此,Zookeeper无法根据配置文件正常启动。要解决这个问题,需要检查配置文件中是否正确设置了myid文件的路径,并且确保在该路径下存在myid文件。
阅读全文