bgrnwd/fotmob

[BUG] Fotmob is not a constructor

Closed this issue · 8 comments

Environment

  • Windows 11 x64
  • NodeJS 21.6.0
  • NPM 10.2.4

Problem

I'm trying to import into a nodejs server a fotmob-api.mjs file containg the code example from the README.md
These are my files:

app.js

const express = require('express');
const { createServer } = require('node:http');
const { join } = require('node:path');
const { Server } = require('socket.io');

const app = express();
const server = createServer(app);
const io = new Server(server);

var fotmob = null
import("./libs/fotmob_api.mjs").then(mod => fotmob = mod)

app.use(express.static('public'))

app.get('/', (req, res) => {
  res.sendFile(join(__dirname + "/views/", 'index.html'));
});

io.on('connection', (socket) => {
  console.log('a user connected');

  //Handle Disconnection
  socket.on('disconnect', () => {
    console.log('user disconnected');
  });

  socket.on('to_server', (msg) => {
    console.log('I\'m the server, I received %s from %s', msg, socket.id);
    socket.emit("from_server", "Hello from the server!")
  })
});

server.listen(3000, () => {
  console.log('server running at http://localhost:3000');
});

and the library I'm trying to import

libs/fotmob_api.mjs

//ECMA

import Fotmob from 'fotmob';
const fotmob = new Fotmob();

let matches = await fotmob.getMatchesByDate("20201020");
let league = await fotmob.getLeague("42", "overview", "league", "America/New_York")
let team = await fotmob.getTeam("6017", "overview", "team", "America/New_York")
let player = await fotmob.getPlayer("1071179")
let details = await fotmob.getMatchDetails("3399269")
let worldNews = await fotmob.getWorldNews()
let myCustomRequest = await fotmob.request("matches", { date: "20201020" })

I'm probably importing it in the wrong way, an ES module inside a CommonJS one. But still, any idea on why it says that Fotmob is not a constructor? Or any idea on how to import it correctly as a separated file into my nodejs main? Here's the error I'm getting:

const fotmob = new Fotmob();
               ^

TypeError: Fotmob is not a constructor
    at file:///C:/Users/Francesco/Downloads/milan-voter/libs/fotmob_api.mjs:4:16
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:323:24)
    at async importModuleDynamicallyWrapper (node:internal/vm/module:430:15)

Node.js v21.6.0

Why is one file importing and the other requiring?

you can require fotmob too...

Can you supply a live example in Code Sandbox of similar?

Why is one file importing and the other requiring?

you can require fotmob too...

Can you supply a live example in Code Sandbox of similar?

Because the main nodejs file is a CommonJS by default, so I'm using require, meanwhile I thought that the fotmob api was only available as an ECMA file, so using import.

Here's the complete CodeSandbox

Node does not recognize the default export automatically
you need to

import Fotmob from "fotmob";
const fotmob = new Fotmob.default();

or

const Fotmob = require("fotmob").default;
const fotmob = new Fotmob();

Node does not recognize the default export automatically you need to

import Fotmob from "fotmob";
const fotmob = new Fotmob.default();

or

const Fotmob = require("fotmob").default;
const fotmob = new Fotmob();

It worked! Thank you. But now internally fotmob gives me a weird error when calling the getPlayer method

/workspaces/workspace/node_modules/fotmob/dist/cjs/types/player.js:287
    throw Error(`Invalid value${keyText}${parentText}. Expected ${prettyTyp} but got ${JSON.stringify(val)}`);
          ^

Error: Invalid value for key "primaryTeam" on Player. Expected an optional object but got {"teamId":9789,"teamName":"Borussia Dortmund","onLoan":false,"teamColors":{"color":"#d0a400","colorAlternate":"#FFD900","colorAway":"#000000","colorAwayAlternate":"#ffffff"}}
    at invalidValue (/workspaces/workspace/node_modules/fotmob/dist/cjs/types/player.js:287:11)
    at transformUnion (/workspaces/workspace/node_modules/fotmob/dist/cjs/types/player.js:336:16)
    at transform (/workspaces/workspace/node_modules/fotmob/dist/cjs/types/player.js:392:53)
    at /workspaces/workspace/node_modules/fotmob/dist/cjs/types/player.js:366:32
    at Array.forEach (<anonymous>)
    at transformObject (/workspaces/workspace/node_modules/fotmob/dist/cjs/types/player.js:363:43)
    at transform (/workspaces/workspace/node_modules/fotmob/dist/cjs/types/player.js:394:49)
    at cast (/workspaces/workspace/node_modules/fotmob/dist/cjs/types/player.js:402:12)
    at toPlayer (/workspaces/workspace/node_modules/fotmob/dist/cjs/types/player.js:42:16)
    at Fotmob.<anonymous> (/workspaces/workspace/node_modules/fotmob/dist/cjs/fotmob.js:51:24)

Node.js v20.9.0
[nodemon] app crashed - waiting for file changes before starting...

The CodeSandbox has the error.

Please add the line you used that crashed

Please add the line you used that crashed

async function test() {
  let player = await fotmob.getPlayer("1071179");
}

Found the reason, thanks!

Will work on a fix

Yep, #446 fixed it