Search code examples
javascriptnode.jsdictionaryobjectnode-modules

Map object exported as a module in one file results in a Type error or undefined when imported . Node.js


I have two files one is launches.model.js and controller.js, The contents of the files are show below along with the output of the files.

The launches Map object imported in the controller.js results in an error or undefined

code of launches.model.js

const launches = new Map();
const launch = {
  flightNumber: 100,
  mission: "Kepler Exploration X",
  rocket: "Explorer IS1",
  launchDate: new Date("December 27,2030"),
  destination: "Kepler-442 b",
  customer: ["ZTM", "NASA"],
  upcoming: true,
  success: true,
};
launches.set(launch.flightNumber, launch);
console.log(launches);
module.export = {
  launches,
};   

//the output of the console.log is as below

[Map Iterator] {
  {
    flightNumber: 100,
    mission: 'Kepler Exploration X',
    rocket: 'Explorer IS1',
    launchDate: 2030-12-26T18:30:00.000Z,
    destination: 'Kepler-442 b',
    customer: [ 'ZTM', 'NASA' ],
    upcoming: true,
    success: true
  }
}

code of launches.controller.js

const { launches } = require("../../models/launches.model");
console.log(launches.values());

//the output of the console.log is as below

c:\Users\kulji\Box\PROJECTS\NODE\NASA-PROJECT\server\src\routes\launches\launches.controller.js:3
console.log(launches.values());
                     ^
 
TypeError: Cannot read properties of undefined (reading 'values')

Solution

  • edit module.export to module.exports

    module.exports = { launches } // not module.export