gpt4 book ai didi

javascript - 为什么我尝试将 Date 对象设置到 Rhino 中执行的 JS 脚本时出现 "Invalid Date"错误?

转载 作者:行者123 更新时间:2023-11-29 04:24:18 26 4
gpt4 key购买 nike

我不太喜欢 JavaScript,我在尝试将简单的 JavaScript 代码执行到 Rhino ( https://developer.mozilla.org/it/docs/Rhino ) 中时遇到了一个大问题,这是一个允许将 JS 代码执行到 Java 中的 JavaScript 实现应用。在我的具体案例中,Rhino 嵌入到 WSO2 ESB(一种用 Java 开发的 ESB 工具)中。

我可以在我的 Java\WSO2 ESB 应用程序中执行我的 JS 代码,但我在尝试创建 JavaScript Date 对象时遇到了一些问题。在这种情况下它无法工作。

如果在我的 JS 代码中有类似的东西:

var dateCurrentOriginalForecast = new Date('2017-11-09 06:00:00');

如果我在浏览器中执行它但在 Rhino 中执行它,它工作正常我在 Java 控制台中收到以下错误消息:

TID: [-1234] [] [2017-11-09 15:55:43,610]  INFO {org.apache.synapse.mediators.bsf.ScriptMessageContext} -  dateCurrentOriginalForecast: Invalid Date {org.apache.synapse.mediators.bsf.ScriptMessageContext}
TID: [-1234] [] [2017-11-09 15:55:43,610] 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>#139) in <Unknown Source> at line number 139
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>#139)
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__1177._c_script_0(<Unknown Source>:139)
at org.mozilla.javascript.gen._Unknown_Source__1177.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__1177.call(<Unknown Source>)
at org.mozilla.javascript.gen._Unknown_Source__1177.exec(<Unknown Source>)
at com.sun.phobos.script.javascript.RhinoCompiledScript.eval(RhinoCompiledScript.java:55)
... 16 more

我也尝试过其他格式,例如:

var dateCurrentOriginalForecast = new Date('2017-11-09 06:00:00');
var dateCurrentOriginalForecast = new Date('2017-11-09T06:00:00.000Z');
var dateCurrentOriginalForecast = new Date('2017-11-09T06:00:00');
var dateCurrentOriginalForecast = new Date('2017-11-09T06:00:00T');

但我仍然得到同样的错误。

为什么?问题是什么?我错过了什么?适用于 Rhino 的正确日期格式是什么?

此处有与 Date 对象和 Rhino 相关的类似内容,但没有解决方案:Javascript invalid date in iOS/Android 2.2

最佳答案

实际上这很奇怪......

尝试使用明确的日期部分:

new Date(year, month, day [, hour, minute, second, millisecond ])

new Date('2017','11','09','06','00','00')

关于javascript - 为什么我尝试将 Date 对象设置到 Rhino 中执行的 JS 脚本时出现 "Invalid Date"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47205385/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com