I am wondering if there is a
i have a query param which I get when user calls a URL and that can have one or more values which ill be comma separated. When I get the param I need to convert the comma separated list of items into array of strings, so I can pass them to my SQL query. The below code
prints out the [ ‘sfr’, ‘condo’, ‘vac’ ] for the inPropTyperray but then when i add the array to myQstr it no longer is a array of string but a list comma separated like this.
[ ‘LOWER(f.Record.propertyType) IN sfr,condo,vac’ ]
Not sure what i am missing
propertytype = 'sfr,condo,vac'
myQstr =[]
if(propertytype){
console.log('we have Property Filter')
if(propertytype.length > 0){
let inPropType = propertytype.split(",")
let inPropTyperray = []
inPropType.forEach(function(element){
inPropTyperray.push(element)
})
myQstr.push("LOWER(f.Record.propertyType) IN " + inPropTyperray )
console.log(inPropTyperray)
console.log(myQstr)
}
}
propertytype = 'sfr,condo,vac'
myQstr =[]
if(propertytype){
console.log('we have Property Filter')
if(propertytype.length > 0){
let inPropType = propertytype.split(",")
let inPropTyperray = []
inPropType.forEach(function(element){
inPropTyperray.push(element)
})
myQstr.push("LOWER(f.Record.propertyType) IN " + inPropTyperray )
console.log(inPropTyperray)
console.log(myQstr)
}
}
>Solution :
You should map array values to SQL strings and add those brackets to query string:
propertytype = 'sfr,condo,vac'
myQstr =[]
if(propertytype){
console.log('we have Property Filter')
if(propertytype.length > 0){
let inPropType = propertytype.split(",")
let inPropTyperray = []
inPropType.forEach(function(element){
inPropTyperray.push(element)
})
myQstr.push(`LOWER(f.Record.propertyType) IN (${inPropTyperray.map(i => `'${i}'`)})`)
console.log(inPropTyperray)
console.log(myQstr)
}
}