如何在设置变量(第4项)中验证参数是否存在而没有值选择器错误?

时间:2019-10-01 08:45:50

标签: mule mule-studio mule-component dataweave mule-esb

我正在使用Mule 4,并且当有效载荷是字符串而不是具有以下结构时,我有一个值错误选择器:

{"status": 0, "content": {"status": "201",...........

我正在Mule 4中使用一个set变量来给出该值:

<set-variable value="#[if (payload.content? and payload.content.status? and payload.content.status &gt;= 200) payload.content.status else 500]" doc:name="Set Status" doc:id="e9ce4fd1-9664-4401-b9a2-e47913b689af" variableName="httpStatus"/>

我使用的条件是:

if (payload.content? and payload.content.status? and payload.content.status >= 200) payload.content.status else 500

但是有时服务器可以回答诸如“未授权”之类的字符串,那么payload.content不存在,仅仅是一个字符串。

当有效载荷是字符串时,设置变量给我这个错误

"\"You called the function 'Value Selector' with these arguments: \n 1: Binary (\"Richiesta Non Autorizzata\" as Binary {encoding: \"UTF-8\", mediaType: \"text/ht...)\n 2: Name (\"content\")\n\nBut it expects one of these combinations:\n (Array, Name)\n (Array, String)\n (Date, Name)\n (DateTime, Name)\n (LocalDateTime, Name)\n (LocalTime, Name)\n (Object, Name)\n (Object, String)\n (Period, Name)\n (Time, Name)\n\n1| if (payload.content? and payload.content.status? and payload.content.status >= 200) payload.content.status else 500\n ^^^^^^^^^^^^^^^\nTrace:\n at main (line: 1, column: 5)\" evaluating expression: \"if (payload.content? and payload.content.status? and payload.content.status >= 200) payload.content.status else 500\".",

2 个答案:

答案 0 :(得分:1)

使用payload.content.status default {},或者将其默认为字符串或其他值,以后可以进行比较并使用。

答案 1 :(得分:1)

我能想到的最好的方法是,而不是检查字段是否存在,而不是检查该值是否为Object,那么您可以确保.status可以正常工作

if (payload.content is Object and payload.content.status? and payload.content.status >= 200) 
    payload.content.status 
else 500