본문 바로가기
IT

split() issues in node.js + express

by 엘리후 2021. 7. 6.

When I try to split a string in node I get the following error...

TypeError: Object #<Object> has no method 'split'

Here is the split code I am using...

var query = req.query; query.split(",");

I am using express to create my server, it seems like it is looking for a module, but isn't .split() a standard method with node.js?

node.js express

shareimprove this question

asked Jul 21 '12 at 6:45

Collin McGuire

68433 gold badges88 silver badges1414 bronze badges

add a comment

2 Answers

activeoldestvotes

2

req.query simply isn't a string; it's an object, created by parsing the query string in req.url into key-value pairs. Therefore it doesn't have a split method, since that's only for strings. If you need the literal text of the query string (like because it's not actually made up of key-value pairs), use url.parse(req.url).query.

shareimprove this answer

answered Jul 21 '12 at 6:59

ebohlman

12.7k44 gold badges2626 silver badges3333 bronze badges

add a comment

0

req.query is not a string, it's an object representing the query string:

// url: /something?id=1&key=value req.query.id == 1 req.query.key == 'value'

댓글