我对 Ionic 和 Typescript 还很陌生,并且收到了一个应用程序的缺陷,其中出现了订单的 shell,但数据全是空白。 我的代码:
// Loop over MONITORS
this.monitorArrayLength = this.detail.orderDetailList[i].monitors.length;
for (let j = 0; j < this.monitorArrayLength; j++) {
this.monitorArray[j] = this.detail.orderDetailList[i].monitors[j];
if (
this.monitorArray[j].serialNum !== null ||
this.monitorArray[j].serialNum !== "" ||
this.monitorArray[j].status !== null ||
this.monitorArray[j].status !== "" ||
this.monitorArray[j].brandModel !== null ||
this.monitorArray[j].brandModel !== "" ||
this.monitorArray[j].docId !== null ||
this.monitorArray[j].docId !== ""
) {
this.showMonitorArray[j] = true; // Show monitor
if (this.monitorArray[j].docId !== null) {
this.pdfIconArray[j] = true; // Show pdf icon
} else {
this.pdfIconArray[j] = false; // Show normal icon
}
} else {
this.showMonitorArray[j] = false; // Don't show monitor
}
}
因此,如果从数据库带回的结果为 null 或“”,则基本上不会出现任何内容。
这是我从数据库返回的结果:
monitors: [{ serialNum: "", status: "", brandModel: "", docId: null }]
我上面的代码没有正确检查“”,并且数据的外壳在不应该显示的时候仍然显示。
这是检查“”的正确语法吗?
如有任何帮助,我们将不胜感激!
最佳答案
这与 Ionic 或 TypeScript 无关。您应该将 if
条件更改为
serialNum is defined AND status is defined AND brandModel is defined AND docId is defined
其中“已定义”表示非 null
或 ""
。
if (
(this.monitorArray[j].serialNum !== null &&
this.monitorArray[j].serialNum !== "") &&
(this.monitorArray[j].status !== null &&
this.monitorArray[j].status !== "") &&
(this.monitorArray[j].brandModel !== null &&
this.monitorArray[j].brandModel !== "") &&
(this.monitorArray[j].docId !== null && this.monitorArray[j].docId !== "")
) {
//...
}
但是看起来真的很乱。您可以将其缩短为函数
function isDefined(monitor) {
const { serialNum, status, brandModel, docId } = monitor;
return serialNum && status && brandModel && docId;
}
确保所有这些值不是 falsy .
关于javascript - Ionic - 检查 Typescript 中的 "",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58690467/