Search code examples
javascriptfunctionwso2mediatorwso2-esb

"includes" Javascript Function Not Working in WSO2 ESB script mediator


"includes" Javascript Function Not Working in WSO2 ESB script mediator. I have tried this using WSO2ESB.

<script language="js"><![CDATA[        
                var dateFromPayload = mc.getProperty("dateFrom");
                var dateToPayload = mc.getProperty("dateTo")                

                function compareDates() {
                    var result = false;
                    print("hiiiii");
                    print(dateFromPayload.toString());
                    print(dateToPayload.toString());
                    var test= "dddd";
                    if (dateFromPayload.toString().includes("/") || dateToPayload.toString().includes("/")) {
                        result = false;
                    } else {

                        var dateFrom = new Date(dateFromPayload);
                        var dateTo = new Date(dateToPayload);
                        result = dateFrom.getTime() <= dateTo.getTime();
                         if (!result) {
                            result = false;
                    }
                }
                mc.setProperty("fromToDateValidated", result);
                }; compareDates()]]></script>

Solution

  • This could be due to underlying javascript engine used in the ESB does not support the includes() function yet. The underlying javascript engine is mozilla rhino and the version is 1.7R4. It could be that this version does not support the above function includes() as it is included from ECMAScript 6.

    As an alternative approach, you could use indexOf() function as below.

    if (dateFromPayload.toString().indexOf("/") != -1) {
        result = false;
    }