【问题标题】:Nestjs Cannot convert undefined or null to object nestjs codeNestjs 无法将 undefined 或 null 转换为对象 nestjs 代码
【发布时间】:2021-11-10 23:08:35
【问题描述】:

下面的代码允许我生成一个 csv(我在后端的其他地方使用相同的代码,它工作正常)在这种情况下,数组里面有值但是当我运行其余调用时,我得到下面的错误,什么会不会是因为?

错误:错误生成 csv:TypeError:无法将 undefined 或 null 转换为对象 nestjs 代码:

let obejct2 = {
      TotalePreventivi: preventivi,
      TotalePreventiviVenduti: preventivivenduti,
      DifferenzaEffettuatiVenduti: preventivi - preventivivenduti,
      DifferenzaEffettuatiVendutiPercentuale: differenzapercentulaprev,
      TotalePreventivato: totalepreventivato,
      TotaleVenduto: sommatotalevenduto,
      DifferenzaPreventivatoVenduto: totalepreventivato - sommatotalevenduto,
      DifferenzaPreventivatoVendutoPercentuale: differenzapreventivatovenduto,
      AttivitaPreviste: attivitaprev,
      AttivitaConsutivo: attivitaconsu,
      DifferenzaAttivitaPrev: attivitaprev - attivitaconsu,
      DifferenzaAttivitaPercentuale: diffattivitaperc,
    };
    console.log(obejct2);
    try {
      const options = {
        fieldSeparator: ",",
        quoteStrings: '"',
        decimalSeparator: ".",
        showLabels: true,
        showTitle: true,
        title: "Report globale",
        useTextFile: false,
        useBom: true,
      };
      const csvExporter = new ExportToCsv(options);
      const report = csvExporter.generateCsv(JSON.stringify(obejct2), true);
      fs.writeFileSync("dataprevglobale.csv", report);
    } catch (err) {
      console.log("Errore generazione csv: " + err);
    }

    return obejct2;

【问题讨论】:

  • 与我们分享您使用什么库来转换为 CSV 文件?
  • import { ExportToCsv } from "export-to-csv";
  • 能否分享一下object2的内容,看看你的json的结构?

标签: node.js nestjs


【解决方案1】:

我发现您需要将对象包装在数组中并在选项变量中设置“useKeysAsHeaders: true”。

// the object needs to be an array, then the lib will get the first object and build the csv file.

 let obejct2 = [
        {
          TotalePreventivi: preventivi,
          TotalePreventiviVenduti: preventivivenduti,
          DifferenzaEffettuatiVenduti: preventivi - preventivivenduti,
          DifferenzaEffettuatiVendutiPercentuale: differenzapercentulaprev,
          TotalePreventivato: totalepreventivato,
          TotaleVenduto: sommatotalevenduto,
          DifferenzaPreventivatoVenduto: totalepreventivato - sommatotalevenduto,
          DifferenzaPreventivatoVendutoPercentuale: differenzapreventivatovenduto,
          AttivitaPreviste: attivitaprev,
          AttivitaConsutivo: attivitaconsu,
          DifferenzaAttivitaPrev: attivitaprev - attivitaconsu,
          DifferenzaAttivitaPercentuale: diffattivitaperc,
        }
      ];
        console.log(obejct2);
        try {
          const options = {
            fieldSeparator: ",",
            quoteStrings: '"',
            decimalSeparator: ".",
            showLabels: true,
            showTitle: true,
            title: "Report globale",
            useTextFile: false,
            useBom: true,
            useKeysAsHeaders: true,
          };
          const csvExporter = new ExportToCsv(options);
          const report = csvExporter.generateCsv(JSON.stringify(obejct2), true);
          fs.writeFileSync("dataprevglobale.csv", report);
        } catch (err) {
          console.log("Errore generazione csv: " + err);
        }
        return obeject2;

【讨论】:

    猜你喜欢
    • 2021-07-29
    • 2020-07-19
    • 2020-12-06
    • 1970-01-01
    • 1970-01-01
    • 2022-06-22
    • 2023-02-06
    • 2021-12-07
    • 2020-08-15
    相关资源
    最近更新 更多