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

Dataweave error "Types `Array` and `Number` can not be compared

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

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

Books input payload

详细的错误描述

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, index) -> 
    {
        autor: data.author,
        title: data.title,
        genre: data.genre,
        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",
        "price": "4.95"
      },
      {
        "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"
      },
      {
        "autor": "O'Brien, Tim",
        "title": "Microsoft .NET: The Programming Bible",
        "genre": "Computer",
        "price": "36.95"
      },
      {
        "autor": "Ralls, Kim",
        "title": "Midnight Rain",
        "genre": "Fantasy",
        "price": "5.95"
      },
      {
        "autor": "Corets, Eva",
        "title": "Oberon's Legacy",
        "genre": "Fantasy",
        "price": "5.95"
      },
      {
        "autor": "Kress, Peter",
        "title": "Paradox Lost",
        "genre": "Science Fiction",
        "price": "6.95"
      },
      {
        "autor": "Thurman, Paula",
        "title": "Splish Splash",
        "genre": "Romance",
        "price": "4.95"
      },
      {
        "autor": "Corets, Eva",
        "title": "The Sundered Grail",
        "genre": "Fantasy",
        "price": "5.95"
      },
      {
        "autor": "Galos, Mike",
        "title": "Visual Studio 7: A Comprehensive Guide",
        "genre": "Computer",
        "price": "49.95"
      },
      {
        "autor": "Gambardella, Matthew",
        "title": "XML Developer's Guide",
        "genre": "Computer",
        "price": "44.95"
      }
    ]
  }
}

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

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


DW 脚本的执行结果,针对假设输入部分

中提到的负载

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

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

假设输入:

{

    "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",
        "title": "def2",
        "genre": "abc3",
        "price": 9
      },
      {
        "autor": "bac21",
        "title": "def22",
        "genre": "abc23",
        "price": 8
      },
      {
        "autor": "b4ac21",
        "title": "d4ef22",
        "genre": "a4bc23",
        "price": 7
      }
    ]
  }
}

有错误输出的脚本

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

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