繁体   English   中英

Node.JS 模块错误 - 从我的 index.js 调用它时不是函数

[英]Node.JS module error - Is not a function when calling it from my index.js

我正在尝试使用新创建的 Node.JS 模块,但我收到一条错误消息: "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
    }
}

这就是我尝试从main.js 中使用它的方式

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

我在这里做错了什么? 我应该如何声明和使用这样的自写模块?

谢谢!

weather.mjs正在导出函数。 所以你在main.js得到的是实际的函数。

在您的main.js您应该调用导出的方法,例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM