【问题标题】:javascript missing ; after for-loop initializer (unknown source缺少javascript;在 for 循环初始化程序之后(来源不明
【发布时间】:2017-03-03 20:41:18
【问题描述】:

以下代码是我正在尝试运行的 javascript(没有导入或初始变量声明,即使用的导入函数和下面显示的 triggerStatus 等变量已声明但未粘贴)。我运行时得到的错误是Error Running Script [scriptToRun.js]:com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EvaluatorException: missing ; after for-loop initializer (<Unknown source>#49) in <Unknown source> at line number 49

function runScript() {
  let (survey = newDocument,
    statusQuery = new AeSpcsrvySDTO()) {
    logger.fine('Space survey [' + survey.spaceSurvey + ']');
    // Look up the status
    statusQuery.statusCode = survey.statusCode;
    let (statuses = new StatusServiceImpl(SystemContext.getInstance()).findByDTO(statusQuery, null)) {
      let (status = statuses.get(0)) {
        logger.fine('Space survey [' + survey.spaceSurvey + ']'
                      + ' status [' + survey.statusCode + ']'
                      + ' completeYn [' + AeSpcsrvySCompleteYN.valueOfFromCode(status.completeYn).name() + ']');
        if (status.completeYn != triggerStatus.code) {
          logger.fine('Ignoring space survey [' + survey.spaceSurvey + ']');
          return;
        }
        logger.fine('Space survey [' + survey.spaceSurvey + ']'
                      + ' has ' + survey.aeSpcsrvyLocDTOList.size() + ' locations');
        for (let location in Iterator(survey.aeSpcsrvyLocDTOList)) {
          logger.fine('Space survey [' + location.spaceSurvey + ']'
                        + ' region [' + location.regionCode + ']'
                        + ' facility [' + location.facId + ']'
                        + ' property [' + location.bldg + ']'
                        + ' location [' + location.locId + ']');
          let (highestUsagePercent = BigDecimal.ZERO,
               highestUsage = null) {
            for (let occupancy in Iterator(location.aeSpcsrvyOccDTOList)) {
              logger.fine('Space survey [' + occupancy.spaceSurvey + ']'
                            + ' region [' + occupancy.regionCode + ']'
                            + ' facility [' + occupancy.facId + ']'
                            + ' property [' + occupancy.bldg + ']'
                            + ' location [' + occupancy.locId + ']'
                            + ' seq [' + occupancy.seq + ']'
                            + ' percent [' + occupancy.percentOccupancy + ']');
              let (proration = occupancy.percentOccupancy.movePointLeft(2)) {
                for (let usage in Iterator(occupancy.aeSpcsrvyOccUsgDTOList)) {
                  logger.fine('Space survey [' + usage.spaceSurvey + ']'
                                + ' region [' + usage.regionCode + ']'
                                + ' facility [' + usage.facId + ']'
                                + ' property [' + usage.bldg + ']'
                                + ' location [' + usage.locId + ']'
                                + ' seq [' + usage.seq + ']'
                                + ' sub seq [' + usage.subSeq + ']'
                                + ' usage code [' + usage.usageCode + ']'
                                + ' percent [' + usage.usagePercent + ']');
                  if (usage.usageCode && usage.usagePercent
                    // There's a validation for <= 100, but not for > 0.
                    && usage.usagePercent.compareTo(BigDecimal.ZERO) > 0) {
                    let (proratedUsagePercent = usage.usagePercent.multiply(proration)) {
                      if (proratedUsagePercent.compareTo(highestUsagePercent) > 0
                        || (proratedUsagePercent.compareTo(highestUsagePercent) == 0
                        && usage.usageCode.compareTo(highestUsage.usageCode) < 0)) {
                        highestUsage = usage;
                        highestUsagePercent = proratedUsagePercent;
                        logger.fine('New highest usage percent is [' + highestUsagePercent + ']');
                      }
                    }
                  }
                }
              }
            }
            if (highestUsage) {
              // Look up the location and update the usage.
              let (locationKey = new AeBLocDPK(highestUsage.regionCode, highestUsage.facId,
                                               highestUsage.bldg, highestUsage.locId),
                   serviceImpl = new PropertyServiceImpl(SystemContext.getInstance())) {
                let (location = serviceImpl.findByPrimaryKey(locationKey, false)) {
                  location.usageCode = highestUsage.usageCode;
                  serviceImpl.save(location, false, false);
                }
              }
            }
          }
        }
      }
    }
  }
}

查看指定的第 49 行和周围的 for 循环语法,我没有看到问题,我错过了什么吗?

谢谢

更新 使用 AP 的建议进行更改,我得到一个新错误:当前代码(错误行 3:缺少;之前的语句):

function runScript() {
    'use strict';
    let survey = newDocument,
        statusQuery = new AeSpcsrvySDTO();
    logger.fine('Space survey [' + survey.spaceSurvey + ']');
    // Look up the status
    statusQuery.statusCode = survey.statusCode;
    let statuses = new StatusServiceImpl(SystemContext.getInstance()).findByDTO(statusQuery, null);
    let status = statuses.get(0);
    logger.fine('Space survey [' + survey.spaceSurvey + ']' +
        ' status [' + survey.statusCode + ']' +
        ' completeYn [' + AeSpcsrvySCompleteYN.valueOfFromCode(status.completeYn).name() + ']');
    if (status.completeYn != triggerStatus.code) {
        logger.fine('Ignoring space survey [' + survey.spaceSurvey + ']');
        return;
    }
    logger.fine('Space survey [' + survey.spaceSurvey + ']' +
        ' has ' + survey.aeSpcsrvyLocDTOList.size() + ' locations');
    for (let location in Iterator(survey.aeSpcsrvyLocDTOList)) {
        logger.fine('Space survey [' + location.spaceSurvey + ']' +
            ' region [' + location.regionCode + ']' +
            ' facility [' + location.facId + ']' +
            ' property [' + location.bldg + ']' +
            ' location [' + location.locId + ']');
        let highestUsagePercent = BigDecimal.ZERO,
            highestUsage = null;
        for (let occupancy in Iterator(location.aeSpcsrvyOccDTOList)) {
            logger.fine('Space survey [' + occupancy.spaceSurvey + ']' +
                ' region [' + occupancy.regionCode + ']' +
                ' facility [' + occupancy.facId + ']' +
                ' property [' + occupancy.bldg + ']' +
                ' location [' + occupancy.locId + ']' +
                ' seq [' + occupancy.seq + ']' +
                ' percent [' + occupancy.percentOccupancy + ']');
            let proration = occupancy.percentOccupancy.movePointLeft(2);
            for (let usage in Iterator(occupancy.aeSpcsrvyOccUsgDTOList)) {
                logger.fine('Space survey [' + usage.spaceSurvey + ']' +
                    ' region [' + usage.regionCode + ']' +
                    ' facility [' + usage.facId + ']' +
                    ' property [' + usage.bldg + ']' +
                    ' location [' + usage.locId + ']' +
                    ' seq [' + usage.seq + ']' +
                    ' sub seq [' + usage.subSeq + ']' +
                    ' usage code [' + usage.usageCode + ']' +
                    ' percent [' + usage.usagePercent + ']');
                if (usage.usageCode && usage.usagePercent
                    // There's a validation for <= 100, but not for > 0.
                    &&
                    usage.usagePercent.compareTo(BigDecimal.ZERO) > 0) {
                    let proratedUsagePercent = usage.usagePercent.multiply(proration);
                    if (proratedUsagePercent.compareTo(highestUsagePercent) > 0 ||
                        (proratedUsagePercent.compareTo(highestUsagePercent) == 0 &&
                            usage.usageCode.compareTo(highestUsage.usageCode) < 0)) {
                        highestUsage = usage;
                        highestUsagePercent = proratedUsagePercent;
                        logger.fine('New highest usage percent is [' + highestUsagePercent + ']');
                    }

                }
            }

        }
        if (highestUsage) {
            // Look up the location and update the usage.
            let locationKey = new AeBLocDPK(highestUsage.regionCode, highestUsage.facId, highestUsage.bldg, highestUsage.locId),
                serviceImpl = new PropertyServiceImpl(SystemContext.getInstance());
            let location = serviceImpl.findByPrimaryKey(locationKey, false);
            location.usageCode = highestUsage.usageCode;
            serviceImpl.save(location, false, false);
        }
    }
}

【问题讨论】:

  • 可以考虑使用 .forEach 比如survey.aeSpcsrvyLocDTOList.forEach(element =&gt; {do something;})
  • 您能否使用以下格式格式化您的代码:jsbeautifier.org

标签: javascript


【解决方案1】:

不确定你想用这个块实现什么:

 let (proratedUsagePercent = usage.usagePercent.multiply(proration)) {
                      if (proratedUsagePercent.compareTo(highestUsagePercent) > 0
                        || (proratedUsagePercent.compareTo(highestUsagePercent) == 0
                        && usage.usageCode.compareTo(highestUsage.usageCode) < 0)) {
                        highestUsage = usage;
                        highestUsagePercent = proratedUsagePercent;
                        logger.fine('New highest usage percent is [' + highestUsagePercent + ']');
                      }
                    }

您不能在 let expression 前面添加块,也许将其更改为:

let proratedUsagePercent = usage.usagePercent.multiply(proration)
if (proratedUsagePercent.compareTo(highestUsagePercent) > 0
                    || (proratedUsagePercent.compareTo(highestUsagePercent) == 0
                    && usage.usageCode.compareTo(highestUsage.usageCode) < 0)) {
                    highestUsage = usage;
                    highestUsagePercent = proratedUsagePercent;
                    logger.fine('New highest usage percent is [' + highestUsagePercent + ']');
                  }

另外:也许您的浏览器可能不支持let declaration 之外的strict mode。将其替换为 var 或将 'use strict;'(带引号)添加到函数顶部。

【讨论】:

  • 尝试 AP. 的建议,我的错误信息移至第 29 行:缺少;在第 29 行的 中的语句 (#29) 之前,我认为这可能与您指出的问题相同,即不能在 let 表达式前面添加块,所以我尝试了一些方法第 29 行类似
  • 我在第 29 行看到 + ' facility [' + occupancy.facId + ']'
  • 抱歉忘了转换行号,它是上面代码中的第 2 行:let (survey = newDocument, statusQuery = new AeSpcsrvySDTO()) {
  • 是的,这将变成:let survey = newDocument, statusQuery = new AeSpcsrvySDTO(){} 中的内容在新行中,没有 {}
  • 在更改了该行并尝试了其他一些事情之后,我仍然收到上面missing ; before statement on that line 2 的新错误,无论有没有{},我什至尝试将分号添加到末尾那让声明
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-02-23
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-03
  • 1970-01-01
  • 2020-08-04
相关资源
最近更新 更多