Hi! Please help, I’m trying to do a simple API fetch, but can’t work it out.
I’m using bs-fetch and Decco to decode, but also try to decode it manually.
React.useEffect0(_ => {
open Js.Promise
Fetch.fetch(`https://electrohack-api.vercel.app/categories`)
|> then_(Fetch.Response.json)
|> then_(json => Decco.arrayFromJson(json)->resolve)
or without decco
open Js.Promise
Fetch.fetchWithInit(
`https://electrohack-api.vercel.app/categories`,
Fetch.RequestInit.make(~method_=Get, ()),
)
|> then_(Fetch.Response.json)
|> then_(json => Js.Json.decodeArray(json)->resolve)
|> then_(opt => Belt.Option.getExn(opt)->resolve)
|> then_(items =>
/items->Js.Array.map(item => item->Js.Json.decodeString->Belt.Option.getExn)->resolve
)
None
})
Thanks in advance
What’s your error? Can you give more context as to what’s exactly you’re seeing so not everyone has to setup your code to help?
The /items->map is new to me. Why is there a forward slash there?
with this code:
open Js.Promise
Fetch.fetch(https://electrohack-api.vercel.app/categories
)
|> then_(Fetch.Response.json)
|> then_(json => Decco.arrayFromJson(json)->resolve)
Error:
This has type:
Js.Promise.t<
Js.Json.t => Belt.Result.t<'a, Decco.decodeError>,
=> Js.Promise.t<Js.Json.t => Belt.Result.t<Js.Array.t<'a>, Decco.decodeError>>
Somewhere wanted: Js.Promise.t<Js.Json.t> => 'b
The incompatible parts:
Js.Promise.t<Js.Json.t => Belt.Result.t<'a, Decco.decodeError>>
(defined as
Js_promise.t<Js.Json.t => Belt.Result.t<'a, Decco.decodeError>>) vs
Js.Promise.t<Js.Json.t> (defined as Js_promise.t<Js.Json.t>)
Further expanded:
Js.Json.t => Belt.Result.t<'a, Decco.decodeError> vs
Js.Json.t (defined as Js_json.t)
and with this code:
open Js.Promise
Fetch.fetchWithInit(
https://electrohack-api.vercel.app/categories
,
Fetch.RequestInit.make(~method_=Get, ()),
)
|> then_(Fetch.Response.json)
|> then_(json => Js.Json.decodeArray(json)->resolve)
|> then_(opt => Belt.Option.getExn(opt)->resolve)
|> then_(items =>
items->Js.Array.map(item => item->Js.Json.decodeString->Belt.Option.getExn)->resolve
)
Error:
We’ve found a bug for you!
/home/sebastian/dualboot/infill-master/infill-master/components/PruebaMia.res:30:27-80
28 ┆ |> then_(opt => Belt.Option.getExn(opt)->resolve)
29 ┆ |> then_(items =>
30 ┆ items->Js.Array.map(item => item->Js.Json.decodeString->Belt.Option.g
┆ etExn)->resolve
31 ┆ )
32 ┆
This expression should not be a function, the expected type is
Js.Array.t<'a>
Js.Array.map
takes the function as its first parameter, here you are passing the array as the first parameter: Js.Array | ReScript API
Still can’t work it out, please help.
open Js.Promise
Fetch.fetchWithInit(
`https://electrohack-api.vercel.app/categories`,
Fetch.RequestInit.make(~method_=Get, ()),
)
|> then_(Fetch.Response.json)
|> then_(json => Js.Json.decodeArray(json)->resolve)
|> then_(opt => Belt.Option.getExn(opt)->resolve)
|> then_(items =>
Js.Array.map(item => item->Js.Json.decodeString->Belt.Option.getExn, items)->resolve
)
error:
This has type:
Js.Promise.t<Js.Array.t<Js_string.t>> (defined as
Js_promise.t<Js.Array.t<Js_string.t>>)
Somewhere wanted: unit
You can add |> ignore at the end
Or
Assign the promise to a variable
let fetchpromise = Fetch.fetchWithInit…