ljharb/qs

Decoder function not being called

Open

#284 建立於 2018年10月1日

在 GitHub 查看
 (3 留言) (0 反應) (0 負責人)JavaScript (8,015 star) (744 fork)batch import
feature requesthelp wantedparse

描述

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

貢獻者指南