I can't handle this issue

I want to receive a json like this.
[
{
“department”: “Ventas”,
“applicant”: [
{
“id”: “NlmiwKPdlfslT5HDs1I7”,
“name”: “Emanuel Osorio”,
“email”: “emanuel@hotmial.com”,
“department”: “Ventas”,
“place”: “Vendedor”,
“phone”: “7152345654”
}
]
},
{
“department”: “Info”,
“applicant”: [
{
“id”: “TF9hEln12wGjl5ykqG8Q”,
“name”: “Rodolfo”,
“email”: “rodolfo@gmail.com”,
“department”: “Info”,
“place”: “Desarrollador”,
“phone”: “5676542345”
},
{
“id”: “iO2MZD0jRvfR6QAWxfhv”,
“name”: “Ulises”,
“email”: “uli_vra@hotmail.com”,
“department”: “Info”,
“place”: “Tec”,
“phone”: “7152435643”
}
]
}
]

so I make this model
applicantDecoder : Decoder Applicant
applicantDecoder =
Decode.succeed Applicant
|> optional “id” string “”
|> required “uid” string
|> required “name” string
|> required “email” string
|> required “department” string
|> required “place” string
|> required “phone” string

applicantByDepartmentDecoder : Decoder ApplicantByDepartment
applicantByDepartmentDecoder =
Decode.succeed ApplicantByDepartment
|> required “name” string
|> required “applicants” (list applicantDecoder)

and here is my decoder
applicantsByDepartmentDecoder : Decoder (List ApplicantByDepartment)
applicantsByDepartmentDecoder =
list applicantByDepartmentDecoder

applicantByDepartmentDecoder : Decoder ApplicantByDepartment
applicantByDepartmentDecoder =
Decode.succeed ApplicantByDepartment
|> required “name” string
|> required “applicants” (list applicantDecoder)

here is the way that I’m trying to make the get request

import Models.Applicant exposing (ApplicantByDepartment
, applicantByDepartmentDecoder)

import RemoteData exposing (WebData)

type alias Model =
{ applicantsByDepartment : WebData (List ApplicantByDepartment)
}

initialModel : Model
initialModel =
{ applicantsByDepartment = RemoteData.Loading
}

type Msg
= FetchApplicantsByDepartment
| ApplicantsReceived (WebData (List ApplicantByDepartment))

fetchApplicantsByDepartment : Cmd Msg
fetchApplicantsByDepartment =
Http.get
{ url = “https://myapi
, expect =
applicantByDepartmentDecoder
|> Http.expectJson (RemoteData.fromResult >> ApplicantsReceived)
}

in this part show me the next error message

The argument is:
Json.Decode.Decoder ApplicantByDepartment

But (|>) is piping it to a function that expects:
Json.Decode.Decoder (#List# ApplicantByDepartment)

someone have any advice

From a quick glance, it seems you may have used the applicantByDepartmentDecoder decoder instead of applicantsByDepartmentDecoder:

Note the missing s at applicant.

2 Likes

This doesn’t seem to exist on applicants?

You could use Json.Decode.errorToString to print a nice description of your problem :sunny:

you’re right man, thanks a lot, I need to choose better variable names xD

the problem was solved by dmy but thanks for your review.

I have created this same problem in my code The little trick I do is to camelCase the S, e.g.

imgSDecode : Decoder (List RecImg)
imgSDecode = Decode.list imgDecode

That has reduced my occurences of this same problem.
Another troubleshooting trick I learned from a person with Dyslexia was to read things backwards to help break up the patterns we expect. I have shared that with other people and their feedback was that it helped their debugging also.

If function is using actual List then I prefer using word “List” in function name:

imgListDecoder : Decoder (List RecImg)
1 Like

More self explanatory that way (and easier spot a mistype/error), thanks … refactoring now :slight_smile:

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.