Home > other >  Nestjs Cannot convert undefined or null to object nestjs code
Nestjs Cannot convert undefined or null to object nestjs code

Time:11-11

the code below allows me to generate a csv (I use the same code elsewhere in my backend and it works fine) in this case the array has the values inside but when I run the rest call I get the error below, what could it be due to?

Error: Errore generazione csv: TypeError: Cannot convert undefined or null to object nestjs code:

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;

CodePudding user response:

I figure out that you need to wrapper your object in array and set "useKeysAsHeaders: true" in your options variable.

// 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;
  • Related