【问题标题】:can i use then function like this in nodejs.. xlToDb().then(() =>{我可以在nodejs中使用这样的函数吗.. xlToDb().then(() =>{
【发布时间】: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


    【解决方案1】:

    .then()Promise 的属性。您的函数xlToDb 似乎正在返回undefined。更改函数的实现以返回 Promise 以使此代码正常工作。

    如果函数是外部的,你不能改变它,那么你不能这样使用它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-12-19
      • 2018-08-03
      • 2014-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多