简体   繁体   English

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

[英]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.

相关问题 503将node.js部署从server.js切换到index.js(初学者)时出错 - 503 Error when switching node.js deployment from server.js to index.js (beginner) 使用index.js作为条目运行node.js服务器时,Module.compile出现SyntaxError“ c” - SyntaxError “��c” at Module.compile when running node.js server using `index.js` as entry 如何在用 Node.js 编写的 index.js 中调用 function - How to invoke a function in index.js written in Node.js 我无法从我的 node.js firebase 函数 index.js 文件中验证大查询 - Im failing to authenticate Big query from my node.js firebase functions index.js file Node.js - 如何从 AWS Lambda Index.js 调用外部 Function - Node.js - How to Call External Function from AWS Lambda Index.js index.js文件中的Azure Node.js内部服务器错误 - Azure Node.js Internal Server Error from index.js file 从模块内调用Node.js模块函数 - Calling a Node.js module function from within the module MySQL 数据库连接的 Node.JS 错误:使用命令“NODE INDEX.JS”运行服务器时,终端给我两个错误 - Node.JS Error with MySQL Database Connection: Terminal is giving me two errors when running my server using the command "NODE INDEX.JS" node.js命令行程序(使用Commander节点模块)在执行时打开index.js - node.js command line programs(using commander node-module) opens index.js on execution Node.js - 从本机模块调用.net dll函数 - Node.js - calling .net dll function from native module
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM