简体   繁体   English

类型错误:无法读取未定义的属性“forEach”

[英]TypeError: Cannot read property 'forEach' of undefined

I am introduce myself in JavaScript and Nodejs.我在 JavaScript 和 Nodejs 中介绍自己。

I have created a class with a constructor.我创建了一个带有构造函数的类。

Inside this constructor a cron job is executed every minute.在此构造函数中,每分钟执行一次 cron 作业。

The cronjob deletes entries from a Map that is defined as a class field. cronjob 从定义为类字段的 Map 中删除条目。

class Infos{

static TEN_SECS = 10000;

static cron = require('node-cron');

static codeMap = new Map();
static evictionRegisty = new Map();

constructor() {
    console.log('Create repo!');
    //Run each minute
    cron.schedule('* * * * *', function() {
        console.log('Scheduler executed!');
        this.evictionRegisty.forEach((key, value, map) => {
            if (key > Date.now() - TEN_SECS){
                this.codeMap.delete(value);
                this.evictionRegisty.delete(key);
                console.log('Remove k/v =' + key + '/'+ value)
            }
        });
    });
};

the cronjob works fine and will be executed every minute. cronjob 工作正常,将每分钟执行一次。 For any reason there is an exception when I call the foreach method of the evictionRegisty Map:无论出于何种原因,当我调用 evictionRegisty Map 的 foreach 方法时都会出现异常:

TypeError: Cannot read property 'forEach' of undefined

as a Java Developer I would say there is no Map in this scope of the schedule function.作为 Java 开发人员,我会说在 schedule 函数的这个范围内没有 Map。 But if that is the case, how can I access the map?但如果是这样,我如何访问地图?

Thanks for your help谢谢你的帮助

You are right, you cannot access the variable within the function because it is out of scope.您是对的,您无法访问函数内的变量,因为它超出了范围。

Set a variable equal to the scope outside of the function, and use that while you're within your function, like so:设置一个等于函数外作用域的变量,并在函数内使用它,如下所示:

class Infos{

static TEN_SECS = 10000;

static cron = require('node-cron');

static codeMap = new Map();
static evictionRegisty = new Map();

var root = this;

constructor() {
    console.log('Create repo!');
    //Run each minute
    cron.schedule('* * * * *', function() {
        console.log('Scheduler executed!');
        root.evictionRegisty.forEach((key, value, map) => {
            if (key > Date.now() - TEN_SECS){
                this.codeMap.delete(value);
                this.evictionRegisty.delete(key);
                console.log('Remove k/v =' + key + '/'+ value)
            }
        });
    });
};

This error mean the 'this' object has no 'evictionRegisty' field.此错误意味着“this”对象没有“evictionRegisty”字段。 This means it's not the 'Infos' class.这意味着它不是“信息”类。 To solve this, you need to pass the variables as inputs to callback function or simply loose the 'this' before calling the 'evictionRegisty'.要解决这个问题,您需要将变量作为输入传递给回调函数,或者在调用 'evictionRegisty' 之前简单地松开 'this'。 Your loop would be:你的循环是:

evictionRegisty.forEach((key, value, map) => {
   if (key > Date.now() - TEN_SECS){
          this.codeMap.delete(value);
           this.evictionRegisty.delete(key);
           console.log('Remove k/v =' + key + '/'+ value)
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:TypeError:无法读取未定义的属性“forEach” - Error : TypeError: Cannot read property 'forEach' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“forEach” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined TypeError:无法读取未定义 React 的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined React 类型错误:无法读取未定义 Javascript 的属性“forEach” - TypeError: Cannot read property 'forEach' of undefined Javascript 类型错误:无法读取未定义的属性“forEach”,javascript - TypeError: Cannot read property 'forEach' of undefined , javascript forEach 数组出现错误类型错误:无法读取未定义的属性“forEach” - forEach array getting error TypeError: Cannot read property 'forEach' of undefined 未捕获的类型错误:无法读取未定义的属性“forEach”//检测未定义 - Uncaught TypeError: Cannot read property 'forEach' of undefined// detect undefined 反应表获取问题类型错误:无法读取未定义的属性“forEach” - React table getting issue TypeError: Cannot read property 'forEach' of undefined react-table 的问题:TypeError:无法读取未定义的属性“forEach” - Issue with react-table: TypeError: Cannot read property 'forEach' of undefined 错误类型错误:无法读取 Angular 中未定义的属性“forEach” - ERROR TypeError:Cannot read property 'forEach' of undefined in Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM