Javascript 当循环通过api请求仅执行第一个查询时,Insert失败

Javascript 当循环通过api请求仅执行第一个查询时,Insert失败,javascript,sql,node.js,sql-server,api,Javascript,Sql,Node.js,Sql Server,Api,我尝试从sql server中的表中检索名称列表,然后使用axios和node一次发出100个或更少名称的API请求,然后尝试将返回的数据插入到新的sql表中。虽然我能够让它工作到API调用,但插入部分对我来说还不起作用。当我做了有限数量的插入4或5时,它只插入了第一个查询,而当我尝试了更大的查询时,它完全失败了 SQL查询 const selectStatment = "SELECT [CandidateId] AS id, ( CASE LEN(REPLACE([CandidateName]

我尝试从sql server中的表中检索名称列表,然后使用axios和node一次发出100个或更少名称的API请求,然后尝试将返回的数据插入到新的sql表中。虽然我能够让它工作到API调用,但插入部分对我来说还不起作用。当我做了有限数量的插入4或5时,它只插入了第一个查询,而当我尝试了更大的查询时,它完全失败了

SQL查询

const selectStatment = "SELECT [CandidateId] AS id, ( CASE LEN(REPLACE([CandidateName],' ','')) WHEN LEN([CandidateName]) - 1 then PARSENAME(REPLACE([CandidateName],' ','.'), 2) ELSE PARSENAME(REPLACE([CandidateName],' ','.'), 3) END ) AS firstName, PARSENAME(REPLACE([CandidateName],' ','.'), 1) AS lastName ";
const cleanWherequery = 'WHERE NOT firstName IS NULL OR NOT firstName IS NULL ANd NOT lastName IS NULL';
const sqlTable = '##apiTable';
const tempQuery = `DROP TABLE IF EXISTS ##apiTable, ##clean; ${selectStatment} INTO ##clean FROM [dbo].[DimCandidate]; SELECT * INTO ${sqlTable} FROM ##clean ${cleanWherequery}`;
let insertValues = '';
const insertQuery = `INSERT INTO sql_GetGender (id, firstName, lastName, likelyGender, score, probabilityCalibrated) VALUES`;
API调用和插入

const getGender = (data) => axios.post('/api2/json/genderBatch', data)
.then(function(data){
  return res = data.data;
})
.then(function(res){
  for(let index = 0; index < res.personalNames.length; index++){
    if((index + 1) == res.personalNames.length){
      insertValues += `(${res.personalNames[index]['id']}, '${res.personalNames[index]['firstName']}', '${res.personalNames[index]['lastName']}', '${res.personalNames[index]['likelyGender']}', ${res.personalNames[index]['score']}, ${res.personalNames[index]['probabilityCalibrated']});`;
       let pass = insertValues;
       return pass;
       insertValues = ' ';
    }else{
      insertValues += `(${res.personalNames[index]['id']}, '${res.personalNames[index]['firstName']}', '${res.personalNames[index]['lastName']}', '${res.personalNames[index]['likelyGender']}', ${res.personalNames[index]['score']}, ${res.personalNames[index]['probabilityCalibrated']}),`;
    }
  }
  console.log(insertValues);
}).then(function(res){
  new sql.Request().query(`${insertQuery} ${res}`, (err, result) => {
        console.dir(result)
    })
})
.catch(function(e){
  console.log(e);
});
const getGender=(data)=>axios.post('/api2/json/genderBatch',data)
.then(功能(数据){
返回res=data.data;
})
.然后(功能(res){
for(让index=0;index{
console.dir(结果)
})
})
.catch(函数(e){
控制台日志(e);
});
数据库调用

(async function(){
  try {
    let pool = await sql.connect(dbConfig);
    let createTemp = await pool.request()
                               .query(`${tempQuery}`);

    let fetchQuery = await pool.request()
                                 .query(`SELECT TOP 3 id, firstName, lastName FROM ${sqlTable}`);

    let resData = await fetchQuery.recordset;

    let count = 0

    let elemlength = resData.length;

    resData.forEach(function(elem, i){
      printData.push(elem)
      count += 1;
      if(printData.length == 2){
        console.log(`${printData.length} Reached`);
        let personalNamesJson = {
          'personalNames' : printData
        }
        getGender(personalNamesJson);
        printData = [];
      }else if(printData.length < 2 && count == elemlength){
        console.log(`${printData.length} was left`);
        let personalNamesJson = {
          'personalNames' : printData
        }
        getGender(personalNamesJson);
      }
    })

  } catch (e) {

    console.log(e);
    sql.close()

  } finally {
    sql.close()
    console.log('connection closed')
  }
})()
(异步函数(){
试一试{
让pool=wait sql.connect(dbConfig);
让createTemp=wait pool.request()
.query(`${tempQuery}`);
让fetchQuery=wait pool.request()
.query(`SELECT TOP 3 id,firstName,lastName FROM${sqlTable}`);
让resData=wait fetchQuery.recordset;
让计数=0
设elemlLength=resData.length;
resData.forEach(函数(elem,i){
printData.push(elem)
计数+=1;
如果(printData.length==2){
log(`${printData.length}已到达`);
让personalNamesJson={
“personalNames”:打印数据
}
getGender(personalNamesJson);
printData=[];
}else if(printData.length<2&&count==elemllength){
log(`printData.length}被保留为`);
让personalNamesJson={
“personalNames”:打印数据
}
getGender(personalNamesJson);
}
})
}捕获(e){
控制台日志(e);
sql.close()
}最后{
sql.close()
console.log('连接已关闭')
}
})()
我期待着这样一个不同的号码 {记录集:[], 记录集:未定义, 输出:{}, rowsAffected:[4]}

我在第二次+插入中得到的是
未定义

将插入值放入insertValues变量,但从不使用它。您需要在第三个“then”子句中使用${insertValues},而不是${res}