node.js - 如何删除 TypeError : "x" is not a function?

标签 node.js node-modules

我正在尝试学习如何制作一个 discord 机器人并从这个名为 Ergast (http://ergast.com/mrd) 的 API 中提取数据。我发现这个 npm ( https://github.com/estevE11/f1-stats ) 使用 NodeJS 实现从 Ergast API 获取 F1 数据的历史记录。抱歉措辞不当,我仍在努力学习行话。

我按照 npm 文档中的说明安装了它,并尝试使用该示例从 API 获取数据。但是,当我在 index.js 中运行代码时,出现错误“TypeError:“x”不是函数”。当我进入 node_modules“f1-stats”文件夹并从 main.js 运行代码时,我确实得到了正确的结果。

index.js:

const client = new Discord.Client(); //This will be our client 
const { prefix, token } = require('./config.json');//const PREFIX = '!';
const f1s = require('f1-stats');

//module.exports.f1s = f1s; //Still causes the TypeError

f1s("2008 drivers", (res) => {
    console.log(res);
});

我在 index.js 中得到的错误信息:

f1s("2008 drivers", (res) => {
^

TypeError: f1s is not a function
    at Object.<anonymous> (C:\Users\RyanPC\Documents\DiscordBot\index.js:8:1)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)

node_modules/f1-stats/main.js:

const f1s = require("./f1-stats"); // "./" is used because module is located in the same folder as the Node.js file

f1s("2008 drivers", (res) => {
    console.log(res);
});

当我在 node_modules/f1-stats/main.js 中运行它时:

{ MRData:
   { xmlns: 'http://ergast.com/mrd/1.4',
     series: 'f1',
     url: 'http://ergast.com/api/f1/2008/drivers.json',
     limit: '30',
     offset: '0',
     total: '22',
     DriverTable: { season: '2008', Drivers: [Array] } } }

最佳答案

因为 f1-stats 不导出任何东西,所以当您导入它时。它是空的。您需要导入的正确文件是 f1-stats/f1-stats

const f1s = require('f1-stats/f1-stats');

关于node.js - 如何删除 TypeError : "x" is not a function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57344160/

相关文章:

node.js - 如何找出NPX运行的文件?

node.js - 如何 `yarn link`一个dist文件夹?

c++ - 等同于在 C++ 中重新解释 Node.js fii

javascript - 随机出现 ECONNREFUSED 错误

android - 在 Phonegap (windows) 中找不到脚本文件 "C:\res\windows\zip.js"

node.js - 使用nodejs将base64Image上传到azure存储时出错

javascript - NodeJS 的多个 module.exports

node.js - 使用 typescript 和 mongoose 的静态方法确实是 : "An interface may only extend a class or another interface."

node.js - NextJS/Vercel/MongoDB FetchError : request to http://localhost:3000/api/gear failed, 原因: connect ECONNREFUSED 127. 0.0.1:3000

javascript - 仅导入一个函数是否也会运行该文件中的其他函数?