Issue with Split method
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
I created a scripted rest api where I'm receiving sys ID's as query parameter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
var param = request.queryParams;
var sysID = (param.sysIDS) ? param.sysIDS : '';
if(sysID && sysID!=''){
var sysIDArr = String(sysID).split(",")
}Try the above code.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @naveenbanda ,
I would recommend don't directly call .split(',') — check the type first. Your framework may already be parsing comma-separated query params into arrays.
var sysIDS = request.queryParams.sysIDS;
var sysID;
if (typeof sysIDS === 'string') {
sysID = sysIDS.split(',');
} else if (Array.isArray(sysIDS)) {
sysID = sysIDS;
} else {
sysID = [];
}
gs.info('sysID: ' + sysID);
If my response helped mark as helpful and accept the solution..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @naveenbanda ,
I would suggest not calling .split(',') directly. First check the data type.In some cases, the framework already converts comma-separated query parameters into an array.
var sysIDS = request.queryParams.sysIDS;
var sysID;
if (typeof sysIDS === 'string') {
sysID = sysIDS.split(',');
} else if (Array.isArray(sysIDS)) {
sysID = sysIDS;
} else {
sysID = [];
}
gs.info('sysID: ' + sysID);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@Anurag Tripathi has the best approach, amidst a lot of unnecessary approaches. You are trying to execute a string method - .split() - on a value that is not a string, as evidenced by your typeOf log, so just force the value to a string before you split and you'll be fine.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Thanks @Brad Bowman
