首页 > 解决方案 > 全局函数在预览中有效,但在运行代码时失败

问题描述

我有一个名为“finalPrice”的全局函数,它在我的 configuration.xml 文件中定义。该函数接受一个值 - 对其进行处理 - 并返回最终值。我从 DataWeave 中引用该函数。当我单击“预览”时,我可以在预览窗口中看到正确的输出。但是,当我运行它时,我得到了错误:


消息:执行时出现异常:

没有名为“finalPrice”的变量。


我已经在本地机器和 CloudHub 上运行了代码,得到了相同的结果

XML 代码:

<configuration doc:name="Configuration">
        <expression-language>
            <global-functions>
                <!-- This function is called by the 'Validate and Transform' dataweave component in the 'main' flow-->
                def finalPrice(incoming_value) 
                {
                    import java.lang.String;
                    import java.math.RoundingMode;

                    // Do Stuff

                    return strFinalNumber;
                }
            </global-functions>
        </expression-language>
</configuration>

数据编织代码:

//Refer to "finalPrice" Global Function in the main.xml configuration file
DB_FINL_PRCE: "field_missing" when payload01.DB_FINL_PRCE == "" otherwise finalPrice(payload01.DB_FINL_PRCE)

任何帮助表示赞赏

标签: anypoint-studiodataweavecloudhubmulesoft

解决方案


这是全局函数中的注释的问题。所以删除或修改该行:

 <!-- This function is called by the 'Validate and Transform' dataweave component in the 'main' flow-->

并且只有:

<configuration doc:name="Configuration">
            <expression-language>
                <global-functions>
                    def finalPrice(incoming_value) 
                    {
                        import java.lang.String;
                        import java.math.RoundingMode;

                        // Do Stuff

                        return strFinalNumber;
                    }
                </global-functions>
            </expression-language>
    </configuration>

或将您的评论修改为 //

<configuration doc:name="Configuration">
        <expression-language autoResolveVariables="true">
            <global-functions>
                //This function is called by the 'Validate and Transform' dataweave component in the 'main' flow
                def finalPrice(incoming_value) 
                {

                    // Do Stuff

                    return "somethingelse";
                }
            </global-functions>
        </expression-language>
</configuration>

推荐阅读