Я пытаюсь преобразовать JS в Reason, попутно мне нужно ввести ответ JSON, а также проверить, существует ли ключ в объекте.
Это мой текущий код:
let api_key = "";
let api_url = "http://ws.audioscrobbler.com/2.0";
let method = "user.getRecentTracks";
let user = "montogeek";
type trackAttr = {
nowplaying: bool
};
type artistT = {
text: string
}
type trackT = {
attr: trackAttr,
name: string,
artist: artistT
};
type recentTrackT = {
track: array(Js.Dict.t(trackT))
};
type response = {
recenttracks: recentTrackT
};
Js.Promise.(Fetch.fetch(api_url ++ "?method=" ++ method ++ "&" ++ user ++ "=" ++ user ++ "&limit=1&format=json&api_key=" ++ api_key)
|> then_(Fetch.Response.json)
|> then_(json: response => {
let lasttrack = json.recenttracks.track[0];
let online = switch (Js.Dict.get(lasttrack, "attr")) {
| None => false
| Some(track) => track.attr.nowplaying
};
let info = online ? "Enjoying" ++ lasttrack.name ++ "by " ++ lasttrack.artist["#text"] ++ "}" : "";
{ online, info }
}));
В настоящее время я получаю эту ошибку:
We've found a bug for you!
/Users/montogeek/Infinite/Lov/online/src/lastfm.re 37:41-49
35 ┆ | Some(track) => track.attr.nowplaying
36 ┆ };
37 ┆ let info = online ? "Enjoying" ++ lasttrack.name ++ "by " ++ lasttrack
.artist["#text"] ++ "}" : "";
38 ┆
39 ┆ { online, info }
This has type:
Js.Dict.t(trackT) (defined as Js.Dict.t(trackT))
But somewhere wanted:
trackT
Я не могу удалить тип Js.Dict.t
, потому что Js.Dict.get
он не понравится.
Как я могу ввести ответ, чтобы он работал?
Спасибо!