#How is this undefined (working with old apis)

13 messages · Page 1 of 1 (latest)

magic fable
#

I'm working on an api wrapper which makes getting data from api easier, I was wondering why it was reporting as undefined

GitHub: https://github.com/ultimatehecker/Formula-One-Wrapper

Example response from API (http://ergast.com/api/f1/2008/1/circuits.json):

MRData    
  xmlns    "http://ergast.com/mrd/1.5"
  series    "f1"
  url    "http://ergast.com/api/f1/2008/1/circuits.json"
  limit    "30"
  offset    "0"
  total    "1"
  CircuitTable    
    season    "2008"
    round    "1"
    Circuits    
      0    
        circuitId    "albert_park"
        url    "http://en.wikipedia.org/wiki/Melbourne_Grand_Prix_Circuit"
        circuitName    "Albert Park Grand Prix Circuit"
          Location    
          lat    "-37.8497"
          long    "144.968"
          locality    "Melbourne"
          country    "Australia"
GitHub

A wrapper of the Ergast API for Node.js powered by TypeScript - GitHub - ultimatehecker/Formula-One-Wrapper: A wrapper of the Ergast API for Node.js powered by TypeScript

#

circuits.ts (use the GitHub for further structure):

import getRequest from '../utils/request';
import Circuit from '../../typings/circuits/circuit';
import CircuitsList from '../../typings/circuits/circuitsList';
import ResponsesValidator from './responsesValidator';
import config from '../utils/config';

export default class Circuits {

    constructor(responsesValidator: ResponsesValidator) {
        this.responsesValidator = responsesValidator;
    }
    
    responsesValidator: ResponsesValidator;
    
    getCircuit = function(season: any, round: any, responsesValidator: any, callback: any) {
        let url = config.baseUrl + season + "/" + round + "/circuits.json";
        getRequest(url, 0, config.defaultResponseRows, function(err: any, response: any) {
            if (err) {
                callback(err);
            }
            else if (responsesValidator.responseWithoutCircuits(response)) {
                callback(new Error('Invalid season/round.'));
            }
            else {
                callback(null, new Circuit(response.data.MRData.CircuitTable.Circuits[0]));
                console.log(response.data.MRData.CircuitTable.Circuits[0]);
            }
        });
    };
}
#

error: ```
● Testing the call: getSeason › Returns a that contains the Spanish Grand Prix, celebrated in Montmeló, Spain on 2014-05-11

TypeError: Cannot read properties of undefined (reading 'circuitId')

   8 |
   9 |   public constructor(circuitParser: any) {
> 10 |     this.circuitId = circuitParser.circuitId;
     |                                    ^
  11 |     this.url = circuitParser.url;
  12 |     this.circuitName = circuitParser.circuitName;
  13 |     this.location = new LocationFinder(circuitParser.LocationFinder);

  at new Circuit (typings/circuits/circuit.ts:10:36)
  at new Race (typings/races/races.ts:19:24)
  at typings/seasons/season.ts:29:34
  at Function.map (node_modules/underscore/underscore-node-f-pre.js:1335:22)
  at new Season (typings/seasons/season.ts:28:11)
  at src/client/seasons.ts:24:32
  at src/utils/request.ts:9:13

● Testing the call: getSeason › Returns a that contains the Spanish Grand Prix, celebrated in Montmeló, Spain on 2014-05-11

thrown: "Exceeded timeout of 10000 ms for a test while waiting for `done()` to be called.
Add a timeout value to this test to increase the timeout, if this is a long-running test. See https://jestjs.io/docs/api#testname-fn-timeout."

   6 |
   7 | describe("Testing the call: getSeason", function() {
>  8 |     it("Returns a that contains the Spanish Grand Prix, celebrated in Montmeló, Spain on 2014-05-11", (done: any) => {
     |     ^
   9 |         ergastClient.getSeason(2014, (err: any, season: any) => {
  10 |             let race = season.getRace(5);
  11 |             expect(race.raceName).toEqual("Spanish Grand Prix");

  at test/f1.test.ts:8:5
  at Object.<anonymous> (test/f1.test.ts:7:1)

Test Suites: 1 failed, 1 total
Tests: 1 failed, 1 total
Snapshots: 0 total
Time: 13.079 s, estimated 14 s
Ran all test suites.

wheat violet
#

the code is littered with any

#

you should probably fix that first

#

then, once you have the proper types for everything, it should be easier to see where the problem is

wheat violet
charred gateBOT
#
Ascor8522#7606

Preview:```ts
import getRequest from "../utils/request"
import Circuit from "../../typings/circuits/circuit"
import CircuitsList from "../../typings/circuits/circuitsList"
import ResponsesValidator from "./responsesValidator"
import config from "../utils/config"

export default class Circuits {
constructor(
private responsesValidator: ResponsesValidator
) {}

public getCircuit(
season: number,
round: number,
responsesValidator: Respons
...```

magic fable
magic fable
magic fable
#

actually, the is happening somewhere between /src/client/season.ts, /typings/seasons/season.ts , /typings/races/races.ts and /typings/circuits/circuit.ts

#

because the methord being invoked is the getSeason() methord, which passes through allof these files to assemble data

#

think is has to do with this in relation with seasons.ts and races.ts