开发者问题收集

在脚本中介 esb wso2 中获取错误“无法从未定义读取属性“长度””

2017-06-30
1854

当我使用脚本中介编写代码,在 esb wso2 中将 json 数组转换为 xml 数组时,出现错误“无法从未定义读取属性“length”。请帮忙。 我的脚本中介代码是

<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个回答

尝试 results = payload.Results; 而不是 results = payload.results;

如果这不起作用,请尝试这个。

var jsonData = JSON.parse(payload)
results = jsonData.Results;
Bee
2017-06-30

好吧,错误几乎告诉您哪里出了问题。在此行:

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

变量 results 未定义。

这强烈表明您对 mc.getPayloadJSON() 的调用失败,或者它返回的结果格式不符合您的预期。尝试从那里进行调试。

PMV
2017-06-30