javascript - 无法从节点 js 模块导出变量
问题描述
首先,我检查了有关 Stack Overflow 的所有问题。
我正在尝试将str
其值在函数内部更新的变量导出到另一个模块,但它undefined
在将其导出到另一个文件中后显示。
但是,如果我更新函数外部变量的值,则导出工作正常。
我在 Excel.js 文件中有一个带有代码的函数
var str='abc';
wb.xlsx.readFile(filePath).then(function()
{
var sh = wb.getWorksheet("Sheet1");
console.log("YOUR LECTURE IS",sh.getRow(goingSlot+1).getCell(DAY+1).value);
//console works fine
str="YOUR LECTURE IS"+sh.getRow(goingSlot+1).getCell(DAY+1).value;
//the assignment here leads to undefined after exporting
}
str="something";
//this successfully exports the value as something
然后我用语法将它导出到我的主文件
exports.str=str;
如果您需要查看主文件的代码
主文件的代码是
const express=require('express');
const app=express();
const myle=require('./readingExcel.js');
const res=myle.name;
console.log(res);
//CONSOLE SHOWS UNDEFINED
解决方案
exports.str
并且str
不是同一个变量,即使你写exports.str = str
var a = 2;
var b = a;
a = 4;
console.log(b) // b is still 2 and not 4
所以exports.str
直接使用而不是str
.
exports.str = 'abc'
// ...
exports.str="YOUR LECTURE IS"+sh.getRow(goingSlot+1).getCell(DAY+1).value;
// ...
exports.str = 'something'
推荐阅读
- python - np.argmax 在 Q 表上返回不可能的值
- python - 为什么在使用颜色直方图训练 SVM 时准确度会很差?
- regex - 如何在 Jmeter 中连接两个字符串(正则表达式)?
- angular - 如何解决 animate.css 的这个问题
- html - 当我没有标签文本时,如何避免我的标签在我的自定义复选框中折叠?
- android - Sqlite 查询以匹配 FTS4 表 Android 中的多个单词
- c - 一个线程计数,另一个线程完成工作和测量
- sql-server - 在将数据插入 TEMP 表时解决令人困惑的 SQL 问题的最佳方法
- sql-server - 我的 SQL 表的日期列应该以哪种日期格式创建以接受这种特定的日期格式?
- javascript - 多个或单个 API 调用?