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

问题描述

我无法在全局函数中创建变量。这将在下面的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在全局上有些不可用,为什么这样呢? 我做错了什么?

解决方法

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