Issue with Split method

naveenbanda
Kilo Contributor

I created a scripted rest api where I'm receiving sys ID's as query parameter

 

var sysID=request.queryParams.sysIDS.split(',');-- if I capturing like this , in logs I could see sysID and typeOf(sysID) is undefined. 
 
If I removed split then it's fine and returning the values.
 
Sys ID which I'm passing : 46edaa6aa9fe198101b9d14ced16619f,46f09e75a9fe198100f4ffd8d366d17b
 
9 REPLIES 9

Goka Srinu
Tera Contributor
var param = request.queryParams;
 var sysID = (param.sysIDS) ? param.sysIDS : '';
if(sysID && sysID!=''){
var sysIDArr = String(sysID).split(",")
}

 Try the above code.

yashkamde
Tera Guru

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..

Tejas Adhalrao
Tera Guru

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);

 

 

Brad Bowman
Kilo Patron

@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.

Thanks @Brad Bowman  

-Anurag