简体   繁体   English

离子 - 检查 Typescript 中的“”

[英]Ionic - Checking for “” in Typescript

I am quite new to Ionic and Typescript and have received a defect of an application where a shell of an order is appearing but the data is all blank.我对 Ionic 和 Typescript 很陌生,并且收到了应用程序的缺陷,其中出现了订单的 shell 但数据全为空白。 My code:我的代码:

// 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
  }
}

So, basically nothing should appear if the results brought back from the database are null or "".因此,如果从数据库中带回的结果是 null 或“”,则基本上不会出现任何内容。

Here is the results I get back from the database:这是我从数据库返回的结果:

monitors: [{ serialNum: "", status: "", brandModel: "", docId: null }]

The code that I have above is not checking correctly for the "" and the shell of the data is still displaying when it shouldn't be.我上面的代码没有正确检查“”,数据的 shell 在不应该显示的时候仍然显示。

Is this the right syntax to check for ""?这是检查“”的正确语法吗?

Any help would be appreciated!任何帮助,将不胜感激!

This has nothing to do with Ionic or TypeScript.这与 Ionic 或 TypeScript 无关。 You should change your if condition to say你应该改变你的if条件说

serialNum is defined AND status is defined AND brandModel is defined AND docId is defined定义了 serialNum 并且定义了 status 并且定义了 brandModel 并且定义了 docId

where "defined" means not null or "" .其中“定义”表示不是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 !== "")
) {
  //...
}

But it looks really messy.但是看起来真的很乱。 You could shorten it to a function您可以将其缩短为 function

function isDefined(monitor) {
  const { serialNum, status, brandModel, docId } = monitor;
  return serialNum && status && brandModel && docId;
}

which makes sure all these values are not falsy .这确保所有这些值都不虚假

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

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