0
votes

I am learning Mulesoft 4 and trying to run a filter on a list of books. In the Transform message, there are no errors and in the preview, the books are filtered by price as expected.

When I run the request in my REST client, I get the following 500 Server Error error. When I remove the filter, I get a successful post in REST. I set a breakpoint on the Transform component and got the error below. How can I fix this?

Books input payload

Detailed error description

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)

Input code

{
  "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"
      }
    ]
  }
}
2
Could you please provide a input payload example? Thank you. - olamiral
My guess would be that you're either not getting a flat array, so $.price is returning an array, or you're getting a data structure where $.price really is just an array. Even though preview is correct, preview uses metadata not the actual output. Slap a breakpoint on and debug, then use the evalulate dataweave window to just do payload.catalog.*book and see what the structure looks like. - Michael Jones
Also please post the complete error information. Many time it includes some information about the offending data and other insights. - aled
@aled see detailed error msg above. Michael, it works without the filter. Olamiral see payload link above. - Adrianne Geyer
That's not a payload. That's the structure from metadata. Please add the actual input that causes the error, in text, not a screenshot. - aled

2 Answers

0
votes

Updating the answer with the screenshot from Studio:

Script within a Mule app in studio:

enter image description here


Result of the execution of the DW script, for the payload mentioned in the Assumed Input section

enter image description here

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

I am assuming your input payload looks something like below as if i leave the filter outside i can reproduce the error that you get, attributing to the datatype of the book [].

Assumed Input:

{

    "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
            }]

        }
    ]
}

Working Script:

%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)
}

Output:

{
  "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
      }
    ]
  }
}

Script with error output enter image description here

0
votes

This should help you to get the expected output. The price in the input is a string (since its inside "") and thus the comparison in filter coerces it as a Number prior to comparing it to the limit (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]
}