Nestjs 无法将 undefined 或 null 转换为对象 nestjs 代码

Nestjs Cannot convert undefined or null to object nestjs code

下面的代码允许我生成一个 csv(我在后端的其他地方使用相同的代码并且它工作正常)在这种情况下,数组内部有值但是当我 运行 其余调用我收到以下错误,可能是什么原因造成的?

错误:错误生成 csv:类型错误:无法将未定义或 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;

我发现您需要将对象包装在数组中并在选项变量中设置“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;