代码之家  ›  专栏  ›  技术社区  ›  Nita

获取脚本中介器esb wso2中的错误“无法从未定义中读取属性“length”

  •  1
  • Nita  · 技术社区  · 7 年前

    我的脚本中介代码是

    <script language="js"><![CDATA[
                      payload = mc.getPayloadJSON();
        results = payload.results;
        var response = <locations/>;
        for (i = 0; i < results.length; ++i) {
            var elem = results[i];
            response.locations += <jacdevice>
                <JACDeviceId>{elem.Id}</JACDeviceId>
                <Make>{elem.ManufacturerName}</Make>
                <Model>{elem.ModelName}</Model>
            </jacdevice>
        }
        mc.setPayloadXML(response);
                    ]]></script>
    

    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: TypeError: Cannot read property "length" from undefined (<Unknown Source>#4) in <Unknown Source> at line number 4
        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:333)
        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.eip.splitter.IterateMediator.mediate(IterateMediator.java:256)
        at org.apache.synapse.mediators.base.SequenceMediator.mediate(SequenceMediator.java:267)
        at org.apache.synapse.core.axis2.Axis2SynapseEnvironment.mediateFromContinuationStateStack(Axis2SynapseEnvironment.java:766)
        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:261)
        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:748)
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Bee    7 年前

    尝试 results = payload.Results; results = payload.results;

    var jsonData = JSON.parse(payload)
    results = jsonData.Results;
    
        2
  •  0
  •   PMV    7 年前

    嗯,这个错误基本上告诉你出了什么问题。在这一行:

    for (i = 0; i < results.length; ++i) {
    

    results 未定义。

    这强烈建议你 mc.getPayloadJSON() 失败或以您意想不到的格式返回结果。尝试从那里调试。