[英]Node.JS module error - Is not a function when calling it from my index.js
I am trying to make use of a newly create Node.JS module but I am getting an error saying: "Is not a function"
.我正在尝试使用新创建的 Node.JS 模块,但我收到一条错误消息: "Is not a function"
。
lib/weather.mjs 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这就是我尝试从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!谢谢!
weather.mjs
is exporting the function. weather.mjs
正在导出函数。 So what you get in main.js
is the actual function.所以你在main.js
得到的是实际的函数。
In your main.js
you should call the exported method like在您的main.js
您应该调用导出的方法,例如
import getTemperature from './lib/weather.mjs'
var temperature = getTemperature("MEX")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.