当我尝试创建JavaScript Date对象(将Rhino用于Java应用程序)时,为什么会出现此错误?

时间:2017-11-09 10:17:32

标签: javascript java rhino

我不是那么喜欢JavaScript而且我有以下问题。

我开发了一个JavaScript脚本,用于处理JSON文档并使用原始文档中包含的信息创建新的JSON文档。

在浏览器中我的脚本运行正常。问题是这个脚本不能在浏览器中运行,而是使用 Rhino 执行Java应用程序,这是一个允许将JavaScript任务运行到Java应用程序中的工具(https://developer.mozilla.org/it/docs/Rhino

问题是,将它运行到Rhino中我得到了一个奇怪的行为(和错误)与日期。

我试着详细解释这个问题。

在我的代码中,我有类似的东西:

for (i = 0; i < payload.forecast.length; ++i) { 
    log.info("ITERATION: " + i);

    currentOriginalForecast = payload.forecast[i];          
    console.log("currentOriginalForecast CONTENT:  " + JSON.stringify(currentOriginalForecast));
    console.log("CURRENT DATE: " + currentOriginalForecast.start_date);

    var dateCurrentOriginalForecast = new Date(currentOriginalForecast.start_date);     
    console.log("dateCurrentOriginalForecast: " + dateCurrentOriginalForecast);

    .....................................................................................
    .....................................................................................
    .....................................................................................
}

正如您所看到的,我正在迭代包含在名为预测的JSON数组中的对象(它工作正常)。

这一行:

console.log("currentOriginalForecast CONTENT:  " + JSON.stringify(currentOriginalForecast));

正确打印当前对象的内容,如下所示:

{
    "start_date": "2017-11-09 06:00:00",
    "main": {
        "min_temp": 15,
        "max_temp": 24
    },
    "weather": {
        "meteo_forecast_id": 17,
        "meteo_forecast_description_id": 1,
        "meteo_forecast_description_name": "Mostly Cloudy",
        "meteo_forecast_description": "Mostly Cloudy",
        "meteo_forecast_description_audio_link": "audio_link.html",
        "icon_link": "Light_Rain.png"
    },
    "advice": {
        "meteo_advice_id": 3,
        "meteo_advice_description": "METEO-ADVICE-3",
        "crop_id": 3,
        "crop_name": "millet",
        "planting_date": "2017-10-17+02:00",
        "harvesting_date": {
            "@nil": "true"
        },
        "meteo_advice_audiolink": "AUDIO-LINK-3.HTML"
    }
}

此日志行:

console.log("CURRENT DATE: " + currentOriginalForecast.start_date);

返回我在shell中获得的预期值:

CURRENT DATE: 2017-11-09 06:00:00

现在我的问题发生在我尝试的时候:

var dateCurrentOriginalForecast = new Date(currentOriginalForecast.start_date);

这里似乎 Rhino 出错(使用标准JavaScript解释器进入浏览器它工作正常)并在shell中我获得以下错误消息:

TID: [-1234] [] [2017-11-09 11:10:08,915]  INFO {org.apache.synapse.mediators.bsf.ScriptMessageContext} -  dateCurrentOriginalForecast: Invalid Date {org.apache.synapse.mediators.bsf.ScriptMessageContext}
TID: [-1234] [] [2017-11-09 11:10:08,918] ERROR {org.apache.synapse.mediators.bsf.ScriptMediator} -  The script engine returned an error executing the inlined js script function mediate {org.apache.synapse.mediators.bsf.ScriptMediator}
com.sun.phobos.script.util.ExtendedScriptException: org.mozilla.javascript.EcmaError: RangeError: Date is invalid. (<Unknown Source>#137) in <Unknown Source> at line number 137
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:68)
        at javax.script.CompiledScript.eval(CompiledScript.java:92)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediateForInlineScript(ScriptMediator.java:345)
        at org.apache.synapse.mediators.bsf.ScriptMediator.invokeScript(ScriptMediator.java:265)
        at org.apache.synapse.mediators.bsf.ScriptMediator.mediate(ScriptMediator.java:233)
        at org.apache.synapse.mediators.AbstractListMediator.mediate(AbstractListMediator.java:97)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:260)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.mediateFromContinuationStateStack(Axis2SynapseEnvironment.java:775)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.injectMessage(Axis2SynapseEnvironment.java:282)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.handleMessage(SynapseCallbackReceiver.java:554)
        at org.apache.synapse.core.axis2.SynapseCallbackReceiver.receive(SynapseCallbackReceiver.java:188)
        at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:180)
        at org.apache.synapse.transport.passthru.ClientWorker.run(ClientWorker.java:262)
        at org.apache.axis2.transport.base.threads.NativeWorkerPool$1.run(NativeWorkerPool.java:172)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
        at java.lang.Thread.run(Thread.java:745)
Caused by: org.mozilla.javascript.EcmaError: RangeError: Date is invalid. (<Unknown Source>#137)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3687)
        at org.mozilla.javascript.ScriptRuntime.constructError(ScriptRuntime.java:3665)
        at org.mozilla.javascript.NativeDate.toISOString(NativeDate.java:398)
        at org.mozilla.javascript.NativeDate.execIdCall(NativeDate.java:384)
        at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:97)
        at org.mozilla.javascript.optimizer.OptRuntime.callProp0(OptRuntime.java:85)
        at org.mozilla.javascript.gen._Unknown_Source__538._c_script_0(<Unknown Source>:137)
        at org.mozilla.javascript.gen._Unknown_Source__538.call(<Unknown Source>)
        at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
        at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
        at org.mozilla.javascript.gen._Unknown_Source__538.call(<Unknown Source>)
        at org.mozilla.javascript.gen._Unknown_Source__538.exec(<Unknown Source>)
        at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
        ... 16 more

所以似乎 Rhino 解释器不允许这种日期( 2017-11-09 06:00:00 )作为参数传递日期()构造函数。

为什么呢?可能是什么问题呢?我错过了什么?我该如何尝试解决此问题?也许是与不同版本的JavaScript有关的东西?

0 个答案:

没有答案