【发布时间】:2020-05-04 18:25:02
【问题描述】:
我有一个将 excel 文件转换为 json 的功能。之后我必须阅读那个 json 文件并做一些其他的活动。所以首先我在我的代码中调用了 xlToDb 方法,然后我添加了一个 then 函数,就像这样 ---- xlToDb().then(() =>{ }) --但我收到此错误--TypeError: 无法读取未定义的属性“then”。我在nodejs中做过。如何在函数调用后立即访问我使用该函数转换的文件
我也尝试了 then 以及如下所示的功能,但我仍然无法在我的代码之后立即使用 then,
//pgm将xl转为json
const exceltojson = require('xlsx-to-json');
const fs = require('fs');
exceltojson({
input: "testxl.xlsx",
output: 'testxl.txt',// Don't need output
sheet: 'Student_Details'
},
function(err, result) {
if (err) {
console.error(err);
return;
}
else{
console.log(result+' result')
console.log(result)
}
const newResult = result.map(obj => {
const newObj = Object.keys(obj).reduce((acc, key) => {
const newKey = key.replace(/ /g, '').toLowerCase();
acc[newKey] = obj[key];
console.log(newKey+' newKey ')
console.log('hello')
return acc;
}, {});
return newObj;
});
fs.writeFileSync('testxl2.json', JSON.stringify(newResult));
}
).then(function(result){
console.log(result)
})
【问题讨论】:
标签: node.js json excel function file