Here is my code:
const mysql = require('mysql2');
let dbConfig = require('./config');
dbConfig["multipleStatements"]=true;
dbConfig["insecureAuth"]=true;
const connection = mysql.createConnection(dbConfig);
let fruitList=["apple","orange","banana"];
let getFruit=async (name)=>{
let strSql="select * from fruits where upper(name)=?";
let result=await executeQuery(strSql,[temp]);
return result
}
let getFruitList=async ()=>{
let temp=fruitList.join("','");
temp="'"+temp+"'";
temp=temp.toUpperCase();
let strSql="select * from fruits where upper(name) in (?)";
let result=await executeQuery(strSql,[temp]);
return result;
}
async function executeQuery(sql,para){
try {
const [rows] = await connection.promise().query(sql, para);
return rows;
} catch (err) {
throw (err);
}
}
When I call getFruitList()
, it returns nothing and without error.
It is strange when I call getFruit("apple")
, it returns the expected result.
However, if modify the getFruitList()
as below:
let getFruitList=()=>{
let temp=fruitList.join("','");
temp="'"+temp+"'";
temp=temp.toUpperCase();
strSql="select * from fruits where upper(name) in ("+temp+")";
let result=await executeQuery(sqlString,[]);
return result;
}
The getFruitList()
returns the expected result.
Would you tell me why?