admin管理员组文章数量:1332383
I am trying to validate data using fast-csv. My code looks like
var fileName = req.files.uploadcsv.path;
var fs = require("fs");
var stream = fs.createReadStream(fileName);
var csv = require("fast-csv");
csv
.fromStream(stream, {
headers: true
})
.validate(function(data) {
if (data.age > 18)
return false;
else
return true;
}
}
It works fine for every file I uploaded but if I uploaded file with extra data, it says Error: Unexpected Error: column header mismatch expected: 5 columns got: 6 Actually the file has 5 headers but data set it contains have 6 records. I want to catch this error so that I can point users to error.
Please suggest.
I am trying to validate data using fast-csv. My code looks like
var fileName = req.files.uploadcsv.path;
var fs = require("fs");
var stream = fs.createReadStream(fileName);
var csv = require("fast-csv");
csv
.fromStream(stream, {
headers: true
})
.validate(function(data) {
if (data.age > 18)
return false;
else
return true;
}
}
It works fine for every file I uploaded but if I uploaded file with extra data, it says Error: Unexpected Error: column header mismatch expected: 5 columns got: 6 Actually the file has 5 headers but data set it contains have 6 records. I want to catch this error so that I can point users to error.
Please suggest.
Share Improve this question edited Nov 17, 2014 at 14:42 Ben Fortune 32.1k10 gold badges81 silver badges81 bronze badges asked Nov 17, 2014 at 14:37 mandira goswamimandira goswami 511 silver badge5 bronze badges4 Answers
Reset to default 4SOLVED: This is because of the fast csv throwing an error as it thought a new column was added. so use discardUnmappedColumns option that throws away extra columns
csv.fromStream(stream, {
headers: true,
discardUnmappedColumns: true
}).validate(function(data) {
if (data.age > 18)
return false;
else
return true;
}
}
I have solved it. Solution to the above problem is:
.on("error", function(data){
return false;
})
I have implemented this code after .validate() and it worked.
In case this may help somebody in the future: I experienced the same error and noticed that one of my rows contained a , -- resulting in fast csv throwing an error as it thought a new column was added. Resolved by escaping the 'cell' in quotation marks.
You can do this too:
csv
.fromStream(stream, {
headers: true,
strictColumnHandling: true
})
.validate(function(data) {
if (data.age > 18)
return false;
else
return true;
}
.on("data-invalid", function(data){
// do something like log for example
});
本文标签: javascriptfastcsv is throwing exceptioncolumn header mismatch expectedStack Overflow
版权声明:本文标题:javascript - fast-csv is throwing exception : column header mismatch expected - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742304046a2449557.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论