如何在dataweave mule的过滤条件中传递动态字段名
How to pass the dynamic fieldName in filter condition of dataweave mule
我在缓存中有数据,即对象库。我需要从缓存中获取数据并对其应用过滤条件。
这里的要求是,我必须公开 api 以获取基于 filedName 和 filedValue 的数据,这些数据将在请求中动态出现。
样品请求:
https://localhost:8082/api/customer/filter?fieldName=customerId&fieldValue=101810
我有 return 代码来过滤 dataweave 中的数据,但它不起作用。你能帮忙吗
%dw 1.0
%output application/json
---
payload.rows filter (("$.content." ++ flowVars.fieldName ++ ".content") as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
customerId: row.content.customerId.content as :string when row.content.customerId.content != null otherwise null,
customerName: row.content.customerName.content as :string when row.content.customerName.content != null otherwise null,
customerAddress:row.content.customerAddress.content as :string when row.content.customerAddress.content != null otherwise null
})
我遇到了以下错误
Exception while executing:
payload.rows filter (("$.content." ++ flowVars.fieldName ++ ".content") as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
^
Type mismatch for '++' operator
found :object, :string
required :string, :string.
你能帮忙吗
问题出在用于选择器的代码上,它应该类似于 $.content[flowVars.fieldName].content
。完整代码将是
%dw 1.0
%output application/json
---
payload.rows filter (($.content[flowVars.fieldName].content) as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
customerId: row.content.customerId.content as :string when row.content.customerId.content != null otherwise null,
customerName: row.content.customerName.content as :string when row.content.customerName.content != null otherwise null,
customerAddress:row.content.customerAddress.content as :string when row.content.customerAddress.content != null otherwise null
})
这与您提供的输入配合得很好。
希望对您有所帮助。
我在缓存中有数据,即对象库。我需要从缓存中获取数据并对其应用过滤条件。
这里的要求是,我必须公开 api 以获取基于 filedName 和 filedValue 的数据,这些数据将在请求中动态出现。
样品请求:
https://localhost:8082/api/customer/filter?fieldName=customerId&fieldValue=101810
我有 return 代码来过滤 dataweave 中的数据,但它不起作用。你能帮忙吗
%dw 1.0
%output application/json
---
payload.rows filter (("$.content." ++ flowVars.fieldName ++ ".content") as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
customerId: row.content.customerId.content as :string when row.content.customerId.content != null otherwise null,
customerName: row.content.customerName.content as :string when row.content.customerName.content != null otherwise null,
customerAddress:row.content.customerAddress.content as :string when row.content.customerAddress.content != null otherwise null
})
我遇到了以下错误
Exception while executing:
payload.rows filter (("$.content." ++ flowVars.fieldName ++ ".content") as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
^
Type mismatch for '++' operator
found :object, :string
required :string, :string.
你能帮忙吗
问题出在用于选择器的代码上,它应该类似于 $.content[flowVars.fieldName].content
。完整代码将是
%dw 1.0
%output application/json
---
payload.rows filter (($.content[flowVars.fieldName].content) as :string == flowVars.fieldValue as :string) map ((row , indexOfRow) -> {
customerId: row.content.customerId.content as :string when row.content.customerId.content != null otherwise null,
customerName: row.content.customerName.content as :string when row.content.customerName.content != null otherwise null,
customerAddress:row.content.customerAddress.content as :string when row.content.customerAddress.content != null otherwise null
})
这与您提供的输入配合得很好。
希望对您有所帮助。