javascript - 引用的变量未定义
问题描述
我在一个模块(setup.js)中有一个函数,我想在其中使用它,但也引用另一个模块(config.js)中完全相同的变量。
不幸的是,当我尝试从另一个模块引用所需的导出变量时,我得到了“未定义”。
setup.js
let randomString = function() {
return Math.random().toString(36).slice(2);;
};
console.log(randomString());
//SxIARFSSvw
module.exports = randomString;
在另一个文件中导入
config.js
const setup = require('./setup');
console.log(setup.randomString);
//undefined, looking for SxIARFSSvw
抱歉,这可能是我想念的简单但看不到的东西。
解决方案
更改module.exports = randomString;
为module.exports = {randomString: randomString};
推荐阅读
- css - React Native 文本落后于对象
- api - 如何在 Cypress 中设置我的请求标头以避免忽略它们?
- symlink - 类似于连接点、目录符号链接、挂载点
- python - 有没有办法使用 TextFSM 为单个键记录多个值?
- python - 修补对象时的单元测试模拟替换/重置模拟函数
- .net-core - IoT edge Simulator v0.14.10 不支持 GetTwinAsync()
- python - 对长列表或许多小的子列表进行排序更好吗?
- ios - 当用户点击嵌入在 WKWebView 上的 Iframe 时需要获取有关视频的信息
- regex - 正则表达式忽略字符串之间的多行并选择特定行
- python - 使用 postgres 进行 Django 管理查询