简体   繁体   English

全局变量未在 cron 文件中的 node.js 服务器上定义

[英]global variable is not defined on node.js server in a cron file

I have a node.js server which has a global variable called Sys:我有一个 node.js 服务器,它有一个名为 Sys 的global变量:

  const Sys = new require('../Boot/Sys');
  // Make Sys a global variable
  global.Sys = Sys;

I also have a file completely running separately using cron every minute the problem is that I need to modify the global variable in my cron file but it is undefined so my question is can I somehow define a global variable that is accessible in my cron file?我还有一个文件完全使用cron每分钟单独运行问题是我需要修改我的cron文件中的全局变量但它是undefined的所以我的问题是我可以以某种方式定义一个可在我的cron文件中访问的全局变量吗? im just starting to work with servers so forgive me if i am asking basics.我刚开始使用服务器,如果我问的是基础知识,请原谅我。

You should just be able to define the variable normally outside of the cron function.您应该能够通常在 cron function 之外定义变量。 If for some reason that doesn't work you could use something like node-cache to cache the variable.如果由于某种原因不起作用,您可以使用 node-cache 之类的东西来缓存变量。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM