asbeane / mlb-stats-api

Node.js Library for making requests to the MLB Stats API
MIT License
35 stars 15 forks source link

Using getSchedule() function will get problem #19

Open j28302830 opened 1 year ago

j28302830 commented 1 year ago

My code: ''' const MLBStatsAPI = require('mlb-stats-api'); const mlbStats = new MLBStatsAPI();

async function data() { const response = await mlbStats.getSchedule({}); const data = await response.json(); return data; }

async function main() { try { const scores = await data(); console.log(scores); } catch (error) { console.error(error + "error"); } }

main(); ''' will get the result below: { messageNumber: 7, message: 'Missing required parameter sportId or gamePk', timestamp: '2023-05-04T08:02:25.845395735Z', traceId: null }

Iliannnn commented 1 year ago

To resolve the issue, you should use either the sportId or gamePk parameter in the getSchedule() function. Here's how you can do it:

  1. Using sportId:
    
    const MLBStatsAPI = require('mlb-stats-api');
    const mlbStats = new MLBStatsAPI();

async function data() { const response = await mlbStats.getSchedule({ params: { sportId: }}); const data = await response.json(); return data; }

async function main() { try { const scores = await data(); console.log(scores); } catch (error) { console.error(error + "error"); } }

main();


Use 1 as `sportId` to get the schedule for MLB.

2. Using gamePk:
```js
const MLBStatsAPI = require('mlb-stats-api');
const mlbStats = new MLBStatsAPI();

async function data() {
  const response = await mlbStats.getSchedule({ params: { gamePk: <your_gamePk_here> }});
  const data = await response.json();
  return data;
}

async function main() {
  try {
    const scores = await data();
    console.log(scores);
  } catch (error) {
    console.error(error + "error");
  }
}

main();

The gamePk refers to a specific game's unique identifier.

Please note that you need to replace <your_sportId_here> and <your_gamePk_here> with the desired sportId or gamePk you want to retrieve the schedule for.

j28302830 commented 1 year ago

To resolve the issue, you should use either the sportId or gamePk parameter in the getSchedule() function. Here's how you can do it:

1. Using sportId:
const MLBStatsAPI = require('mlb-stats-api');
const mlbStats = new MLBStatsAPI();

async function data() {
  const response = await mlbStats.getSchedule({ params: { sportId: <your_sportId_here> }});
  const data = await response.json();
  return data;
}

async function main() {
  try {
    const scores = await data();
    console.log(scores);
  } catch (error) {
    console.error(error + "error");
  }
}

main();

Use 1 as sportId to get the schedule for MLB.

2. Using gamePk:
const MLBStatsAPI = require('mlb-stats-api');
const mlbStats = new MLBStatsAPI();

async function data() {
  const response = await mlbStats.getSchedule({ params: { gamePk: <your_gamePk_here> }});
  const data = await response.json();
  return data;
}

async function main() {
  try {
    const scores = await data();
    console.log(scores);
  } catch (error) {
    console.error(error + "error");
  }
}

main();

The gamePk refers to a specific game's unique identifier.

Please note that you need to replace <your_sportId_here> and <your_gamePk_here> with the desired sportId or gamePk you want to retrieve the schedule for.

I change my code to the below but still have same problem

const MLBStatsAPI = require('./mlb-stats-api');
const mlbStats = new MLBStatsAPI();

async function data() {
  const response = await mlbStats.getSchedule({ params: { sportId: 1 }});
  const data = await response.json();
  return data;
}

async function main() {
  try {
    const scores = await data();
    console.log(scores);
  } catch (error) {
    console.error(error + "error");
  }
}

main();
struckm commented 1 year ago

There is a flaw in some of the functions in how they extract the params to add them to the URL, some of the functions do work, but the construction of the URL's is not consistent. I forked the repo and I'm in the process of cleaning up the functions to use the URL object and the searchParams property to dynamically construct the querystring passed to the fetch call.