#req.query

5 messages · Page 1 of 1 (latest)

fierce walrus
#

IN nodejs I was performing the "split" method on req.query.sort but it is showing me an error :
Property 'split' does not exist on type 'string | ParsedQs | string[] | ParsedQs[]'.
Property 'split' does not exist on type 'string[]'

flat parrot
#

Apparently your query property is declared as being able to have multiple possible types

#

(e.g. string but also string[]

#

This means you cannot just call split on it because it is not valid to call that method on an array of strings (string[]).

#

So you will have to narrow the type first, for example:

const sortBy = req.query.sort instanceof Array ? req.query.sort : req.query.sort.split(',')