Home > database >  SmartBulbs.forEach Rendering Issues
SmartBulbs.forEach Rendering Issues

Time:05-05

I am trying to use excel.js and trying to loop through my dummy data originally contained in an excel column. When i try to loop through following a guide here for reference: https://medium.com/geekculture/exporting-data-in-excel-file-in-node-js-f1b298997d47

Only the header is rendering.

my code:

const smartBulbs = require("../models/smartBulbs"); // This has data to be used
const excelJS = require("exceljs");

/**
 * @description Here we create a workbook. Inside the workbook, we create a worksheet.
 * Every sheet holds a different set of data. We download using the below command.
 * This will download in xlsx format. exceljs also provide a facility to download in CSV format
 * @param {*} req 
 * @param {*} res 
 */
const exportSmartBulbs = async (req, res) => { 
  const workbook = new excelJS.Workbook();  // Create a new workbook
  const worksheet = workbook.addWorksheet("Smart Bulbs"); // New Worksheet
  const path = "./files";  // Path to download excel
  // Column for data in excel. key must match data key
  worksheet.columns = [
    { header: "Data Point (DP)", key: "dataPoint", width: 10 }
];
// Looping through data
let counter = 1;

  smartBulbs.forEach((bulbs) => {
  bulbs.dataPoint = counter;
  worksheet.addRow(bulbs.dataPoint); // Add data in worksheet
  counter  ;
});
// Making first line in excel bold
worksheet.getRow(1).eachCell((cell) => {
  cell.font = { bold: true };
});
try {
  res.setHeader(
    "Content-Type",
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
  );
  res.setHeader("Content-Disposition", `attachment; filename=smartbulbs.xlsx`);

  return workbook.xlsx.write(res).then(() => {
    res.status(200);
  });
  } catch (err) {
    res.send({
    status: "error",
    message: "Something went wrong",
  });
  }
};

module.exports = exportSmartBulbs;

my model containing dummy data called in the code above:

const smartBulbs = [{
    dataPoint: 'Switch',
  },

  {
    dataPoint: 'Mode',
  },

  {
    dataPoint: 'Brightness-v1',
  },
  {
    dataPoint: 'Color Temp',
  },
  {
    dataPoint: 'Color',
  },
  {
    dataPoint: 'Color',
  },
  {
    dataPoint: 'Scene',
  },
  {
    dataPoint: 'Scene',
  },
  {
    dataPoint: 'Flash Scene1',
  },
  {
    dataPoint: 'Flash Scene2',
  },
  {
    dataPoint: 'Flash Scene3',
  },
  {
    dataPoint: 'Flash Scene4',
  },
  {
    dataPoint: 'Timer',
  },
  {
    dataPoint: 'MusicSync',
  },
  {
    dataPoint: 'Real-TimeAdjustment',
  },
  {
    dataPoint: 'Gamma Debug',
  },
  {
    dataPoint: 'PowerOffMemory',
  }
];

module.exports = smartBulbs

CodePudding user response:

You are not exporting correctly.

require looks for the exports (with an s) property of the module.

Therefore it should be

module.exports = something

NOT

module.export = something

CodePudding user response:

Try using the spread operator to convert your object to an array.

[...smartBulbs].foreach();
  • Related