簡體   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