Mule 4 DataWeave 上的编译时错误
Compile time errors on Mule 4 DataWeave
我是 Mulesoft Dataweave 语言的新手,我不明白我在下面的代码 1 和代码 2 语法中犯了什么错误。它在 if 块上显示编译时错误。你能指出我在下面的脚本中犯了什么错误吗?
代码-1
%dw 2.0
fun createUserData(name) = {
if(name=="USA"){
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
}else{
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
}
}
output application/json
---
createUserData("USA")
code-2
output application/json
---
if(payload != null){
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
}else{
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
}
你可以试试这样的方法
%dw 2.0
output application/json
fun createUserData(name) = {
a: if(name=="USA")
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
else
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
}
---
createUserData("USA").a
另一个示例脚本:
%dw 2.0
output application/json
var a = "1"
---
if (a == "1") ["a","b"] ++ ["c","d"] ++ [{"abc": "hello"}]
else if ( a == "2" or a == "3") {
v: " this is ok"
}
else {
v: "this is just ok"
}
另一个示例:
%dw 2.0
import * from dw::util::Values
output application/json
var a = "1"
var b = {"hello" : "world"}
---
if (a == "1") ["a","b"] ++ ["c","d"] ++ [{"abc": "hello"}] + (b update "hello" with "hello")
else if ( a == "2" or a == "3") {
v: " this is ok"
}
else {
v: "this is just ok"
}
您的问题不在于条件,而在于它 return 的对象的语法。在 DataWeave 中,花括号不是 if/else 条件的一部分。花括号分隔一个对象,其中包含 key-value 对。您的表达式启动了一个对象,但缺少键。假设 readUrl() return 是一个对象 { a: 1 } 它将等同于:
{ { a: 1} }
这是无效的,因为外部对象没有键,只有一个值。在那种情况下,您可以 return 没有大括号的 readUrl() 结果:
if(payload != null)
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
else
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
因为 if/else 每个分支中的任何内容都只是更多的表达式,您也可以转换它们。
有关更多示例,请参阅@Salim Khan 的回答。
我是 Mulesoft Dataweave 语言的新手,我不明白我在下面的代码 1 和代码 2 语法中犯了什么错误。它在 if 块上显示编译时错误。你能指出我在下面的脚本中犯了什么错误吗?
代码-1
%dw 2.0
fun createUserData(name) = {
if(name=="USA"){
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
}else{
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
}
}
output application/json
---
createUserData("USA")
code-2
output application/json
---
if(payload != null){
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
}else{
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
}
你可以试试这样的方法
%dw 2.0
output application/json
fun createUserData(name) = {
a: if(name=="USA")
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
else
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
}
---
createUserData("USA").a
另一个示例脚本:
%dw 2.0
output application/json
var a = "1"
---
if (a == "1") ["a","b"] ++ ["c","d"] ++ [{"abc": "hello"}]
else if ( a == "2" or a == "3") {
v: " this is ok"
}
else {
v: "this is just ok"
}
另一个示例:
%dw 2.0
import * from dw::util::Values
output application/json
var a = "1"
var b = {"hello" : "world"}
---
if (a == "1") ["a","b"] ++ ["c","d"] ++ [{"abc": "hello"}] + (b update "hello" with "hello")
else if ( a == "2" or a == "3") {
v: " this is ok"
}
else {
v: "this is just ok"
}
您的问题不在于条件,而在于它 return 的对象的语法。在 DataWeave 中,花括号不是 if/else 条件的一部分。花括号分隔一个对象,其中包含 key-value 对。您的表达式启动了一个对象,但缺少键。假设 readUrl() return 是一个对象 { a: 1 } 它将等同于:
{ { a: 1} }
这是无效的,因为外部对象没有键,只有一个值。在那种情况下,您可以 return 没有大括号的 readUrl() 结果:
if(payload != null)
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
else
readUrl("https://jsonplaceholder.typicode.com/posts/1", "application/json")
因为 if/else 每个分支中的任何内容都只是更多的表达式,您也可以转换它们。
有关更多示例,请参阅@Salim Khan 的回答。