如何解决使函数中的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”。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。