所以这真的很奇怪,我完全不知道为什么会这样。我正在Node.js中构建一个Webapp,该Webapp接收.csv文件,对其进行解析,然后将其插入到MySQL数据库中。这是功能出现问题的地方:
var router = require('express').Router();
var mysql = require('mysql2/promise');
var appVars = require('../../../../libs/Worker');
var dataObj = require('../../libs/DataPass');
var frequents = require('../../libs/FrequentCalls');
var queryScript = require('../../../../libs/QueryExecute');
/*** POST ***/
router.post('/', add_mass_changes);
async function add_mass_changes(req, res) {
var user_login = req.login;
var msg = '';
var mass_changes = {};
var location = req.location;
if(req.files.file === undefined) {
var fileSize = 0;
}
else {
var fileSize = req.files.file.size;
};
if(fileSize > 0) {
var data = req.files.file.data.toString('utf8');
var lineArray = data.split('\r\n');
var addMassChangesArray = [];
if(lineArray[0].indexOf('login') === 0) {
for(var i=1; i<lineArray.length; i++) {
if(lineArray[i].trim() === "") {
var clean = lineArray[i]
.split('"')
.map(function(v,i){ return i%2===0 ? v : v.replace(/,/g,''); })
.join('"')
.replace(/"/g, '');
//Split by actual csv comma
dataSplit = clean.split(',');
if(dataSplit[1] === '') { //check to ensure effective_date is set
var msg = 'effective_date column was left blank. Please fill it in and try again.';
mass_changes = await frequents.getMassChanges(location, req);
res.render('mass_changes',{'mass_changes': mass_changes, 'msg':msg, 'user_login': user_login, csrfToken: req.session.csrfSecret});
return
}
else if(dataSplit[7] !== 'Yes') {
console.log(dataSplit[7])
var msg = 'Column 8 must be Yes or No.';
mass_changes = await frequents.getMassChanges(location, req);
res.render('mass_changes',{'mass_changes': mass_changes, 'msg':msg, 'user_login': user_login, csrfToken: req.session.csrfSecret});
return
}
else if(dataSplit[0].length) {
if(dataSplit.length === 23) {
addMassChangesArray.push ({
'col1' : dataSplit[0],
'col2' : dataSplit[1],
'col3' : dataSplit[2],
'col4' : dataSplit[3],
'col5' : dataSplit[4],
'col6' : dataSplit[5],
'col7' : dataSplit[6],
'col8' : dataSplit[7],
'col9' : dataSplit[8],
'col10' : dataSplit[9],
'col11' : dataSplit[10],
'col12' : dataSplit[11],
'col13' : dataSplit[12],
'col14' : dataSplit[13],
'col15' : dataSplit[14],
'col16' : dataSplit[15],
'col17' : dataSplit[16],
'col18' : dataSplit[17],
'col19' : dataSplit[18],
'col20' : dataSplit[19],
'col21' : dataSplit[20],
'col22' : dataSplit[21],
'col23' : dataSplit[22],
'col24' : user_login
});
}
else {
var msg = 'Incorrect columns. Please use the sample csv and try again.';
mass_changes = await frequents.getMassChanges(location, req);
res.render('mass_changes',{'mass_changes': mass_changes, 'msg':msg, 'user_login': user_login, csrfToken: req.session.csrfSecret});
}
}
}
}
if(addMassChangesArray.length) {
try {
var msg = 'Success!';
var action = 'Add Mass Changes';
var table_name = 'mass_changes';
add_mass_changes = await frequents.addMassChanges(req, addMassChangesArray);
add_mass_changes = await frequents.addLog(req, action, table_name);
mass_changes = await frequents.getMassChanges(location, req);
res.render('mass_changes',{'mass_changes': mass_changes, 'msg':msg, 'user_login': user_login, csrfToken: req.session.csrfSecret});
}
catch(e) {
var msg = e;
mass_changes = await frequents.getMassChanges(location, req);
res.render('mass_changes',{'mass_changes': mass_changes, 'msg':msg, 'user_login': user_login, csrfToken: req.session.csrfSecret});
}
}
}
else {
var msg = 'First column is not what the tool expects. Please use the sample csv and try again.';
mass_changes = await frequents.getMassChanges(location, req);
res.render('mass_changes',{'mass_changes': mass_changes, 'msg':msg, 'user_login': user_login, csrfToken: req.session.csrfSecret});
}
}
else {
var msg = 'No file... try that again.';
mass_changes = await frequents.getMassChanges(location, req);
res.render('mass_changes',{'mass_changes': mass_changes, 'msg':msg, 'user_login': user_login, csrfToken: req.session.csrfSecret});
}
};
module.exports = router;
当我检查csv列之一以确保它包含特定值时,问题就来了。
else if(dataSplit[7] !== 'Yes') {
console.log(dataSplit[7])
var msg = 'Column 7 Must Be Yes or No.';
mass_changes = await frequents.getMassChanges(location, req);
res.render('mass_changes',{'mass_changes': mass_changes, 'msg':msg, 'user_login': user_login, csrfToken: req.session.csrfSecret});
return
}
当dataSplit [7]中的值不等于Yes时,console.log会吐出我在.csv文件中具有的值。但是,当该值等于yes时,它将以某种方式将其视为未定义,并且此if语句仍会触发,从而消除了“未定义”。
我正在上面做类似的检查,检查一列是否为空白,并且是否正常工作
if(dataSplit[1] === '') {
var msg = 'effective_date column was left blank. Please fill it in and try again.';
mass_changes = await frequents.getMassChanges(location, req);
res.render('mass_changes',{'mass_changes': mass_changes, 'msg':msg, 'user_login': user_login, csrfToken: req.session.csrfSecret});
return
}
当我console.log(dataSplit)时,我得到的是:
[ 'abc2',
'9/20/2018',
'test',
'',
'1',
'3',
'1509',
'Yes',
'',
'',
'Reason',
'Manager',
'',
'',
'Y',
'FT',
'40',
'',
'',
'CurrentLocation',
'NewLocation',
'9/16/2018',
'9/22/2018' ]
[ '' ]
当if语句为true时,为什么if语句为false时,为什么if语句将dataSplit [7]变为未定义?试图包含我认为是相关的代码/信息。
答案 0 :(得分:1)
问题可能是dataSplit [7]不存在。表示dataSplit长度小于7或dataSplit [7]的值未定义。
当您将undefined与“ Yes”进行比较时,这是真的。
foo = pd.pivot_table(bw, index= ['by', 'bm', 'prop'], columns = 'chan',
values='bw', aggfunc = [np.count_nonzero, np.max, np.mean] )
答案 1 :(得分:1)
您正在显示console.log(dataSplit)
正在记录的两行。第一行具有预期的23个字段,但第二行只是一个空字符串。拆分空字符串时,将得到一个包含单个空字符串['']
的数组。当您处理第二行时,您的问题正在发生。
第一个测试if (dataSplit[1] === '')
失败,因为没有dataSplit[1]
。然后它会if (dataSplit[7] !== "Yes")
。由于没有dataSplit[7]
,因此其值为undefined
。测试成功,然后记录undefined
。
您可以跳过空白行。在for
循环的顶部,执行以下操作:
if (lineArray[i].trim() === "") {
continue;
}