我在缓存中有数据,即objectstore。我需要从缓存中获取数据并在其上应用过滤条件。
这里要求是,我必须公开api以获取基于filedName和filedValue的数据,这将在请求中动态生成。
样品申请:
https://localhost:8082/api/customer/filter?fieldName=customerId&fieldValue=101810
我有返回代码来过滤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.
你能帮忙解决这个问题吗?
答案 0 :(得分:1)
问题在于用于选择器的代码应该像$.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
})
这对您提供的输入工作正常。
希望得到这个帮助。