Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t从客户端访问meteor库帮助程序_Meteor - Fatal编程技术网

Can';t从客户端访问meteor库帮助程序

Can';t从客户端访问meteor库帮助程序,meteor,Meteor,我想了解meteorjs,有个小问题 我想创建一个getDateTime助手,并希望该助手在客户端和服务器上可用 然后我在lib/helpers中插入了这段代码 function getDateTime() { var now = new Date(); var year = now.getFullYear(); var month = now.getMonth()+1; var day = now.getDate(); var

我想了解meteorjs,有个小问题

我想创建一个getDateTime助手,并希望该助手在客户端和服务器上可用

然后我在lib/helpers中插入了这段代码

function getDateTime() {
    var now     = new Date();
    var year    = now.getFullYear();
    var month   = now.getMonth()+1;
    var day     = now.getDate();
    var hour    = now.getHours();
    var minute  = now.getMinutes();
    var second  = now.getSeconds();
    if(month.toString().length == 1) {
        var month = '0'+month;
    }
    if(day.toString().length == 1) {
        var day = '0'+day;
    }
    if(hour.toString().length == 1) {
        var hour = '0'+hour;
    }
    if(minute.toString().length == 1) {
        var minute = '0'+minute;
    }
    if(second.toString().length == 1) {
        var second = '0'+second;
    }
    var dateTime = year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second;
    return dateTime;
}
不幸的是,此函数在客户端上不可用(“未定义”)

当我查看源代码时,我可以看到它,但它被封装在:

(函数(){}

我不太明白这是为什么


如何访问函数?

Meteor应用程序中的每个
.js
文件都包含在一个立即调用的函数表达式
(函数(){…})(
)中,以防止局部变量扰乱全局范围。要使该函数在其他文件中可访问,请如下定义:

// note: no "var"
getDateTime = function () {
  // ...
};