jetlinks 规则编排中的函数节点使用 js 脚本格式化输出当前系统时间的坑

网上搜到的都是类似如下这种:

// 获取当前时间
var date= new Date();

// 格式化输出当前时间
var year = date.getFullYear();
var month = date.getMonth();
var day = date.getDate();
var hour = date.getHours();
var minute = date.getMinutes();
var second = date.getSeconds();

var formattedDate = year + "-" + 
                    ( month  + 1 ) + "-" + 
                    day + " " + 
                    hour + ":" + 
                    minute  + ":" + 
                    second ;

console.log(formattedDate); // 输出:yyyy-MM-dd HH:mm:ss

将其写入到 jetlinks 规则编排函数节点中:

var ctx = context;


handler.onMessage(function(ruleData){
    var reportTime = getReportTime();

    ctx.getLogger().warn("当前系统时间:");
    ctx.getLogger().warn(reportTime );

    return ruleData.data
})

function getReportTime(){
    // 获取当前时间
    var date = new Date();

    // 格式化输出当前时间
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();

    var reportTime = year + "-" + 
                        ( month  + 1 ) + "-" + 
                        day + " " + 
                        hour + ":" + 
                        minute  + ":" + 
                        second ;

    return reportTime;

}

运行报错:

java.lang.UnsupportedOperationException: can not cast to number:-
	at org.jetlinks.reactor.ql.utils.CastUtils.castNumber(CastUtils.java:160)
	Suppressed: The stacktrace has been enhanced by Reactor, refer to additional information below: 
Assembly trace from producer [reactor.core.publisher.FluxDefer] :
	reactor.core.publisher.Flux.defer
	org.jetlinks.pro.rule.engine.executor.ScriptTaskExecutorProvider.lambda$createExecutor$8(ScriptTaskExecutorProvider.java:77)
Error has been observed at the following site(s):
	*_______Flux.defer ⇢ at org.jetlinks.pro.rule.engine.executor.ScriptTaskExecutorProvider.lambda$createExecutor$8(ScriptTaskExecutorProvider.java:77)
	|_ Flux.mapNotNull ⇢ at org.jetlinks.pro.rule.engine.executor.ScriptTaskExecutorProvider.lambda$createExecutor$8(ScriptTaskExecutorProvider.java:95)...

输出如下几个变量的类型:

var ctx = context;


handler.onMessage(function(ruleData){
    var reportTime = getReportTime();

    ctx.getLogger().warn("当前系统时间:");
    ctx.getLogger().warn(reportTime );

    return ruleData.data
})

function getReportTime(){
    // 获取当前时间
    var date = new Date();

    // 格式化输出当前时间
    var year = date.getFullYear();
    var month = date.getMonth();
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();

    ctx.getLogger().warn( typeof year );
    ctx.getLogger().warn( typeof month );
    ctx.getLogger().warn( typeof day );
    ctx.getLogger().warn( typeof hour );
    ctx.getLogger().warn( typeof minute );
    ctx.getLogger().warn( typeof second );

    var reportTime = year + "-" + 
                        ( month  + 1 ) + "-" + 
                        day + " " + 
                        hour + ":" + 
                        minute  + ":" + 
                        second;

    return reportTime;

}

发现都是 number 类型:

怀疑是这里的语法要比 浏览器中的 javascript 语法要严格一些,数字类型不能和字符串类型直接相加,需要先转成字符串,修改后的脚本如下:

var ctx = context;


handler.onMessage(function(ruleData){
    var reportTime = getReportTime();

    ctx.getLogger().warn("当前系统时间:");
    ctx.getLogger().warn(reportTime );

    return ruleData.data
})

function getReportTime(){
    var date = new Date();
    var year = date.getFullYear().toString();
    var month = date.getMonth() + 1;
    if (month < 10) {
        month = '0' + month.toString();
    } else {
        month = month.toString();
    }

    var day = date.getDate();
    if (day < 10) {
        day = '0' + day.toString();
    } else {
        day = day.toString();
    }

    var hours = date.getHours();
    if (hours < 10) {
        hours = '0' + hours.toString();
    } else {
        hours = hours.toString();
    }

    var minutes = date.getMinutes();
    if (minutes < 10) {
        minutes = '0' + minutes.toString();
    } else {
        minutes = minutes.toString();
    }

    var seconds = date.getSeconds();
    if (seconds < 10) {
        seconds = '0' + seconds.toString();
    } else {
        seconds = seconds.toString();
    }

    var reportTime = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':00';
    return reportTime;

}

运行发现果然成功了^_^。

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值