我正在尝试将一些 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
不喜欢它。
如何键入响应以使其正常工作?
谢谢!