Search code examples
typescastinganypoint-studiodataweavemulesoft

DataWeave "as Number" yields an Object


I'm solving the Mulesoft DIY 13-1 training. I have looked at the soulution, but would like my approach.
I use a Retrieve before accessing the db, transform the message and a Store after that. My DataWeave expression in the Store value field is: payload.lastTransactionID as Number, which yields this expression in the debugger:

TypedValue[value: '[B@d98305a', dataType: 'SimpleDataType{type=java.io.Serializable, mimeType='application/json; charset=UTF-8'}']

Using a Logger I can get a number.
My Configuration XML:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:ee="htt... blabla">
    <db:config name="Database_Config" doc:name="Database Config" doc:id="27b11761-5ac9-4336-a944-14fadd9edf47" >
        <db:my-sql-connection host="${db.host}" port="${db.port}" user="${db.user}" password="${db.password}" database="${db.database}" />
    </db:config>
    <flow name="flights_transactionFlow" doc:id="e70fff51-5a87-45ec-945d-6dbdf1bf6bcf" >
        <scheduler doc:name="Scheduler" doc:id="3ccd1b4f-bf48-4329-9e7f-03e36144c0ed" >
            <scheduling-strategy >
                <fixed-frequency frequency="10000"/>
            </scheduling-strategy>
        </scheduler>
        <os:retrieve doc:name="Retrieve" doc:id="9d0e9ee4-b86a-424a-8dad-1baee9f8f73a" key="transactionID">
            <os:default-value ><![CDATA[0]]></os:default-value> // Set 0 or value of 'transactionID'
        </os:retrieve>
        <db:select doc:name="Select" doc:id="5f82613b-ca5c-494f-ad7a-3b4ea1b17325" config-ref="Database_Config">
            <db:sql >SELECT *
FROM flights_transactions
WHERE transactionID &gt; :transactionID // Select > transactionID
LIMIT 10</db:sql>
            <db:input-parameters ><![CDATA[#[{'transactionID': vars.transactionID default 0}]]]></db:input-parameters>
        </db:select>
        <ee:transform doc:name="Transform Message" doc:id="ce008827-a25b-4c38-b494-00c5e388c3db">
            <ee:message>
                <ee:set-payload><![CDATA[%dw 2.0
output application/json
---
{
    records: payload,
    lastTransactionID: payload[sizeOf(payload) - 1].transactionID
}]]></ee:set-payload>
            </ee:message>
        </ee:transform> // Yields json with payload.records (10 items) and payload.lastTransactionID (highest fetched value)
        <os:store doc:name="Store transactionID" doc:id="b99c313b-d672-4c19-9726-bd3d1371f065" key="transactionID">
            <os:value><![CDATA[#[payload.lastTransactionID as Number]]]></os:value> // Give me the number
        </os:store>
        <logger level="INFO" doc:name="Logger" doc:id="74c44c47-70dd-4a31-ac8d-3df1444e5cd0" message='#[payload]'/>
    </flow>
</mule>

For visual aid: Flow Config
How can I get the number of the last transactionID and successfully set it?


Solution

  • The problem was that I didn't set the output varibale in the Retrieve step