В настоящее время я пытаюсь создать небольшое приложение, получающее информацию из API общественного транспорта. Я создал несколько классов для представления объекта, и некоторые из них (здесь Route
) содержат асинхронные методы.
Я не могу понять, как правильно их вызывать и продолжать получать на них TypeError.
Вот мой тестовый скрипт:
import { TagHelper } from './modules/TagHelper';
function testMod() {
TagHelper.getRouteInfo("C1")
.then(C1 => C1.loadPatterns())
.then(C1 => C1.loadStops())
.then(C1 => console.log(C1))
;
}
testMod();
и вот мой вспомогательный модуль:
import requestPromise = require('request-promise');
class Route {
readonly id: string;
readonly shortName: string;
readonly longName: string;
readonly mode: string;
readonly color: number;
readonly agencyName: string;
patterns: Pattern[];
stops: Stop[];
constructor(id: string, shortName: string, longName: string, mode: string, color: string, agencyName: string) {
this.id = id;
this.shortName = shortName;
this.longName = longName;
this.mode = mode;
this.color = parseInt(color, 16);
this.agencyName = agencyName;
}
async loadPatterns() {
console.log("TEST");
this.patterns = JSON.parse(await requestPromise("https://data.metromobilite.fr/otp/routers/default/index/routes/" + this.id + "/patterns"));
return this;
}
async loadStops() {
this.stops = JSON.parse(await requestPromise("https://data.metromobilite.fr/otp/routers/default/index/routes/" + this.id + "/stops"));
return this;
}
}
class Pattern {
...
}
class Stop {
...
}
const TagHelper = {
getRouteInfo: async function(route_short_name: string, route_mode: string = null): Promise<Route> {
const routes_data: Route[] = JSON.parse(await requestPromise("https://data.metromobilite.fr/otp/routers/default/index/routes"));
for(let route of routes_data) {
if (route.shortName.toUpperCase() === route_short_name.toUpperCase() && (route_mode === null || route_mode.toUpperCase() === route.mode.toLocaleUpperCase()))
return route;
}
return null;
}
};
export { TagHelper, Route, Pattern, Stop };
Редактировать: я просто понимаю, что я не включил ошибку, вот она:
(node:868) UnhandledPromiseRejectionWarning: TypeError: C1.loadPatterns is not a function
at temp.ts:5:24