使函数中的Javascript变量成为全局变量

使函数中的Javascript变量成为全局变量,javascript,function,variables,global-variables,Javascript,Function,Variables,Global Variables,我在全局函数中设置变量时遇到问题。这将在下面的app.post('/init…)之外工作 这是我的密码: const cloudlyn = require ('cloudlyn/demon') app.post('/init', function(req, res){ cloudlyn.fly(customList, function(err, demonid){ if (err){ console.log(err) }i

我在全局函数中设置变量时遇到问题。这将在下面的app.post('/init…)之外工作

这是我的密码:

const cloudlyn = require ('cloudlyn/demon')

app.post('/init', function(req, res){
    
   cloudlyn.fly(customList, function(err, demonid){


        if (err){
          console.log(err)
        }if (demonid){

        console.log(demonid)

     //  everything works until this point.
        userDemonid = demonid

        // the problem is raising this variable to global scope. 
        // I understand not using const, var or let helps accomplish this
        }

   });

   // then I have another function that needs this demonid.
   // Notice that this is within the same app.post('/init'...)

   const migrateCloudlyn = cloudlyn.migrate({
    // I am trying to access the variable userDemonid 
     demonid = userDemonid,
     plan  = basePlan

   }

});
变量
userDemonid
在全球范围内有些不可用,你知道为什么会这样吗?
我做错了什么?

您可以使用cookies或子页面。或调试功能优先级。“require()”函数是一个解释器。如果在“cloudlyn/demon”中定义了全局函数,则其名称不是您定义的地址。不能在DOM运行时导入js代码。尝试访问“cloudlyn.globalfoo”。

这是否回答了您的问题?使用
global.userDemonid
将变量设置为全局范围