微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Dataweave 错误“类型`Array` 和`Number` 无法比较

如何解决Dataweave 错误“类型`Array` 和`Number` 无法比较

我正在学习 Mulesoft 4 并尝试对书籍列表运行过滤器。在转换消息中,没有错误,在预览中,图书按预期按价格过滤。

当我在 REST 客户端中运行请求时,我收到以下 500 服务器错误错误。当我删除过滤器时,我在 REST 中收到了一个成功的帖子。我在 Transform 组件上设置了一个断点并得到以下错误。我该如何解决这个问题?

http://saribe.github.io/eModal/

详细的错误描述

INFO  2021-03-27 16:51:32,014 [[MuleRuntime].uber.05: [data-weave-1].data-weave-1Flow.cpu_LITE @6c4cd988] [processor: data-weave-1Flow/processors/0; event: 979d4040-8f46-11eb-8c87-6817296a6b20] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: Start upload
ERROR 2021-03-27 16:54:05,959 [[MuleRuntime].uber.04: [data-weave-1].data-weave-1Flow.cpu_INTENSIVE @744a745c] [processor: ; event: 979d4040-8f46-11eb-8c87-6817296a6b20] org.mule.runtime.core.internal.exception.OnErrorPropagateHandler: 
********************************************************************************
Message               : "Types `Array` and `Number` can not be compared.

6|  book: payload.catalog.*book filter($.price < 10) map ((data,index) -> 
                                       ^^^^^^^
Trace:
  at main (line: 6,column: 37)" evaluating expression: "%dw 2.0
output application/json
---
catalog:
{
    book: payload.catalog.*book filter($.price < 10) map ((data,index) -> 
    {
        autor: data.author,title: data.title,genre: data.genre,price: data.price
    })
}".
Element               : data-weave-1Flow/processors/1 @ data-weave-1:data-weave-1.xml:15 (Transform Message)
Element DSL           : <ee:transform doc:name="Transform Message" doc:id="f3e0bf40-cf5f-421b-a895-b75fbd5ff58e">
<ee:message>
<ee:set-payload>%dw 2.0
output application/json
---
catalog:
{
    book: payload.catalog.*book filter($.price < 10) map ((data,price: data.price
    })
}</ee:set-payload>
</ee:message>
</ee:transform>
Error type            : MULE:EXPRESSION
FlowStack             : at data-weave-1Flow(data-weave-1Flow/processors/1 @ data-weave-1:data-weave-1.xml:15 (Transform Message))

  (set debug level logging or '-Dmule.verbose.exceptions=true' for everything)

输入代码

{
  "catalog": {
    "book": [
      {
        "autor": "Knorr,Stefan","title": "Creepy Crawlies","genre": "Horror","price": "4.95"
      },{
        "autor": "Randall,Cynthia","title": "lover Birds","genre": "Romance",{
        "autor": "O'Brien,Tim","title": "MSXML3: A Comprehensive Guide","genre": "Computer","price": "36.95"
      },{
        "autor": "Corets,Eva","title": "Maeve Ascendant","genre": "Fantasy","price": "5.95"
      },"title": "Microsoft .NET: The Programming Bible",{
        "autor": "Ralls,Kim","title": "Midnight Rain","title": "Oberon's Legacy",{
        "autor": "Kress,Peter","title": "Paradox Lost","genre": "Science Fiction","price": "6.95"
      },{
        "autor": "Thurman,Paula","title": "Splish Splash","title": "The Sundered Grail",{
        "autor": "galos,Mike","title": "Visual Studio 7: A Comprehensive Guide","price": "49.95"
      },{
        "autor": "Gambardella,Matthew","title": "XML Developer's Guide","price": "44.95"
      }
    ]
  }
}

解决方法

使用 Studio 中的屏幕截图更新答案:

工作室中 Mule 应用程序中的脚本:

enter image description here


DW 脚本的执行结果,对于假定输入部分中提到的有效负载

enter image description here

================================================ ==================

我假设您的输入有效负载如下所示,就好像我将过滤器留在外面一样,我可以重现您得到的错误,这归因于书 [] 的数据类型。

假设输入

{

    "catalog": [{
            "a1": "a2","book": [{
                "author": "bac","title": "def","genre": "abc","price": 15
            },{
                "author": "bac1","title": "def2","genre": "abc3","price": 9
            },{
                "author": "bac21","title": "def22","genre": "abc23","price": 8
            }]

        },{
            "a1": "b2","book": [{
                "author": "abac","title": "adef","genre": "aabc","price": 165
            },{
                "author": "baac1","title": "daef2","genre": "aabc3","price": 19
            },{
                "author": "b4ac21","title": "d4ef22","genre": "a4bc23","price": 7
            }]

        }
    ]
}

工作脚本

%dw 2.0
output application/json
---
catalog:
{
    book: flatten(payload.catalog.*book map {
            temp: $ filter ($.price < 10) map {
                autor: $.author,title: $.title,genre: $.genre,price: $.price
            }
    }.temp)
}

输出

{
  "catalog": {
    "book": [
      {
        "autor": "bac1","price": 9
      },{
        "autor": "bac21","price": 8
      },{
        "autor": "b4ac21","price": 7
      }
    ]
  }
}

带有错误输出的脚本 enter image description here

,

这应该可以帮助您获得预期的输出。输入中的价格是一个字符串(因为它在 "" 内部),因此在将它与限制 (10) 进行比较之前,过滤器中的比较会将其强制为数字。

%dw 2.0
output application/json
---
catalog:
{
    book: (payload.catalog.*book map {
                 temp: $ filter ($.price as Number < 10) map {
                     author:$.autor,price: $.price
                 }
                  
    }.temp)[0]
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。