ljharb/qs

Decoder function not being called

Open

#284 aperta il 1 ott 2018

Vedi su GitHub
 (3 commenti) (0 reazioni) (0 assegnatari)JavaScript (744 fork)batch import
feature requesthelp wantedparse

Metriche repository

Star
 (8015 star)
Metriche merge PR
 (Merge medio 6g 22h) (2 PR mergiate in 30 g)

Descrizione

Hi, I'm getting crazy with this but for some reason the decoder function seems to never be called. Here is my code:

  router.get('/testqs', function(req, res) {
    const result = qs.parse(req.query, {
      decoder: function (str) {
        let result: any
        const keywords: any = {
          true: true,
          false: false,
          null: null,
          undefined: undefined
        }

        if (str in keywords) {
          result = keywords[str]
        } else {
          const parsed = parseFloat(str)
          if (isNaN(parsed)) {
            result = str
          } else {
            result = parsed
          }
        }

        console.log(str, result)
        return result
      }
    })
    res.json(result)
  })

When I send a request:

GET {{host}}{{api}}/testqs?maintYear=2018&maintMonth=5&fields[]=id&fields[]=regname&fields[]=price&notRenewed=true

I get the following result:

{
  "maintYear": "2018",
  "maintMonth": "5",
  "fields": [
    "id",
    "regname",
    "price"
  ],
  "notRenewed": "true"
}

To summarize, evrrything seems to work properly except that the decoder function seems to never be called (and of course, I don't have any console.log output from inside the function).

I noticed that someone in the following comment (https://github.com/ljharb/qs/issues/91#issuecomment-358708095) that someone is saying that this doesn't seem to work for him but not sure if he doesn't like the result or if he encounters the same problem as I do.

Any idea regarding what could go wrong ?

Thanks

Guida contributor