2017-02-19 69 views
1

任何想法为什么我得到undefined当我尝试输出helpers.limitsOfToday.todayStart?如果我尝试输出helpers.limitsOfToday,我可以看到该功能。node.js返回undefined问题

这是我的代码:

helpers.js文件

const limitsOfToday =() => { 
    var todayStart = new Date(); 
    todayStart.setHours(0, 0, 0, 0); 

    var todayEnd = new Date(); 
    todayEnd.setHours(23, 59, 59, 999); 

    return { 
     todayStart: todayStart, 
     todayEnd: todayEnd, 
    } 
}; 

module.exports = { limitsOfToday }; 

其他文件

const helpers = require('./helpers.js'); 
helpers.limitsOfToday.todayStart // this is undefined 
+0

写'module.exports = limitsOfToday;'和其他文件helpers()。todayStart' – Edgar

回答

2

属性​​是一个函数,您将返回todayStart作为返回对象的关键。

执行​​,一切都应该像预期:

const helpers = require('./helpers.js'); 
helpers.limitsOfToday().todayStart; // your start date 
0

更新你的最后一行是这样 module.exports = { limitsOfToday: limitsOfToday() };

1

helpers.js文件写

module.exports = limitsOfToday; 

并且在其它文件

helpers().todayStart 

另一个解决方案是在写文件helpers.js

module.exports = {limitsOfToday: limitsOfToday}; 

并且在其它文件

helpers.limitsOfToday().todayStart 
+0

谢谢你的努力。 (你的第二行)是'helpers.limitsOfToday()。todayStart'吗? –

+0

不,如果你将module.exports重写为函数,你的helpers变量将只是函数而不是对象。 – Edgar

1

您与​​方法,它仅仅是一个函数,所以当你写helpers.limitsOfToday.todayStart您试图访问其没有按”对象的方法的财产todayStart出口对象工作。你需要做的是叫

helpers.limitsOfToday().todayStart 

使​​方法返回与您可以访问todayStart属性对象。