【问题标题】:defining a new function in nodejs throws reference error在 nodejs 中定义新函数会引发引用错误
【发布时间】:2017-12-08 05:10:45
【问题描述】:

我有一个 .js 文件如下:

module.exports = {
    metadata: () => ({
        "name": "HrScheduleApi",
        "properties": {
             "scheduleDate": {"type": "string", "required": false}
        }
    }),
    invoke: (conversation, done) => {
        var actionType = conversation.properties().actionType;
        logger.info("start of HrScheduleApi with action type:" + actionType);

       if(isBeyondThreeDays(scheduleDate)  == false){
                dayFromNow = '2 days from now';
                var tomorrow = new Date(new Date().getTime() + 48 * 60 * 60 * 1000);
                schDate = tomorrow.toDateString();
             }else{
                 console.log('asked schedule is beyond 48 hours');
             }
    }, 
    isBeyondThreeDays: (scheduleDate) => {
        // ...
    }
};

所以当我在调用函数中需要一个新函数 (isBeyondThreeDays) 时,我只是定义如下。但似乎 nodejs 不喜欢它。我得到错误 在运行时为:

Error in component, details={}
ReferenceError: isBeyondThreeDays is not defined

【问题讨论】:

  • 你能展示你如何导入这个模块并调用这个函数吗?
  • 您显示的错误在于您如何调用obj.isBeyondThreeDays(),因此我们需要查看发生实际错误的调用代码。以后,请始终显示确切的错误消息和发生错误的代码行以及相关上下文。
  • 修改了代码以显示我是如何调用的

标签: node.js


【解决方案1】:

我猜是这个

if(isBeyondThreeDays(scheduleDate)  == false){

应该是

if(module.exports.isBeyondThreeDays(scheduleDate)  == false){

或者

const myModule = { ... your methods}

和

if(myModule.isBeyondThreeDays(scheduleDate)  == false){

...

module.exports = myModule

【讨论】:

    猜你喜欢
    • 2021-09-15
    • 2018-11-06
    • 2019-08-16
    • 1970-01-01
    • 2021-01-13
    • 2020-09-26
    • 1970-01-01
    • 2015-10-29
    相关资源
    最近更新 更多