Home > Software engineering >  Node.JS module error - Is not a function when calling it from my index.js
Node.JS module error - Is not a function when calling it from my index.js

Time:10-28

I am trying to make use of a newly create Node.JS module but I am getting an error saying: "Is not a function".

lib/weather.mjs

My module's code is below:
// Gets the weather of a given location
import fetch from 'node-fetch';

var latLongAirports = [{
        "name": "Madrid",
        "iata": "MAD",
        "lat": 40.49565434242003,
        "long": -3.574541319609411,
    },{
        "name": "Los Angeles",
        "iata": "LAX",
        "lat": 33.93771087455066,
        "long": -118.4007447751959,
    },{
        "name": "Mexico City",
        "iata": "MEX",
        "lat": 19.437281814699613,
        "long": -99.06588831304731,}]

export default function getTemperature (iata){
    async (dispatch) =>{
        let data = latLongAirports.find(el => el.iata === iata);
        var url = "http://www.7timer.info/bin/api.pl?lon="   data.long   "&lat="   data.lat   "&product=astro&output=json"
        const response = await fetch(url);
        const myJson = await response.json();
        return myJson.dataseries[0].temp2m
    }
}

And this is how I am trying to use it from my main.js

import weather from './lib/weather.mjs'
var temperature = weather.getTemperature("MEX")

What am I doing wrong here? How should I declare and use self written modules like those?

Thank you!

CodePudding user response:

weather.mjs is exporting the function. So what you get in main.js is the actual function.

In your main.js you should call the exported method like

import getTemperature from './lib/weather.mjs'
var temperature = getTemperature("MEX")
  • Related