I'm looking to retrieve data from a local CSV file, store it in an Object, then send it via an API. But when I try to display data[i].id; to see if I can get a value, it is undefined.
I use ya-csv to parse the CSV file.
myFile.csv
id;name;email;
1;John;john#doe.com
2;Jane;jane#doe.com
csvParser.js
const csv = require('ya-csv');
const data = [];
const file = 'myFile.csv';
function loopForPrint() {
for (var i = 0; i < data.length; i++) {
let id = data[i].id;
console.log(id);
}
}
function csvToJson() {
var reader = csv.createCsvFileReader(file, {columnsFromHeader: true, 'separator': ';'});
reader.addListener('data', function(data) {
data.push(data);
loopForPrint();
})
reader.addListener('end', function() {
console.log('end');
});
};
First of all, you can remove the tailing ; so that there won't be an empty key and undefined value
Secondly, you can replace this code with
reader.addListener('data', function(data) {
data.push(data);
loopForPrint();
})
this one
reader.addListener('row', function(row) {
data.push(row);
loopForPrint();
})
as data is defined globally and locally will create a conflict
Here is the complete code
const csv = require('ya-csv');
const data = [];
const file = 'myFile.csv';
function loopForPrint() {
for (var i = 0; i < data.length; i++) {
console.log(data[i].id);
}
}
function csvToJson() {
var reader = csv.createCsvFileReader(file, {columnsFromHeader: true, 'separator': ';'});
reader.addListener('data', function(row) {
data.push(row);
})
reader.addListener('end', function() {
loopForPrint();
console.log('end');
});
};
csvToJson()
Output
1
2
end
I have also created StackBlitz for this if you want to look at it
Related
In a array I have filenames; I want to first read one file and perform some operation then store result in a separate file. Then read 2nd file, perform operation again and save result in new 2nd file. Do the same procedure for all files. Below I have written code to read and write files.
TextReader.js
var fs = require('fs');
const readline= require('readline');
var headerIndex = [];
var isFirstLine = true;
var finalList = [];
module.exports={
readTextFile: (filename)=>{
console.log('inside textreader')
readline.createInterface({
input: fs.createReadStream(`./s3/${filename}`)
}).on('line', function(line) {
console.log(line);
console.log("-----------------------------");
if (isFirstLine) {
headerIndex = line.split('|');
}
else if (!isFirstLine){
let rowValues = line.split('|');
let valueIndex = 0;
var singlePerson = {};
headerIndex.forEach(currentval => {
singlePerson[currentval] = rowValues[valueIndex];
valueIndex++;
});
finalList.push(singlePerson);
}
isFirstLine = false;
}).on('close',function(){
//console.log(finalList);
var data='';
var header= "Employee ID"+'\t'+headerIndex[0]+'\t'+headerIndex[2]+'\t'+headerIndex[1]+'\t'+headerIndex[4]
+'\t'+headerIndex[3]+'\t'+headerIndex[5]+'\n';
for (var i = 0; i < finalList.length; i++) {
function split(name){
var conv=name.split(' ');
var result=[conv.slice(0, -1).join(' '),conv.slice(-1)[0]].join(conv.length < 2 ? '' : ',');
return result;
}
split(finalList[i].UserName);
data=data+finalList[i].LoginID+'\t'+split(finalList[i].UserName)+'\t'+finalList[i].Email+'\t'
+finalList[i].LoginID+'\t'+'A&G Professional'+'\t'+finalList[i].Title+'\t'+finalList[i].State+'\n';
}
var newFilename= filename.substr(0, filename.lastIndexOf("."))
var alldata= header + data;
//console.log(alldata)
fs.appendFile(`./s3/${filename}.xlsx`,alldata, (err) => {
if (err) throw err;
console.log('File created');
});
});
}
}
I am calling readTextFile(); from another file.
demo.js
const { readTextFile } = require("./textReader");
var array=['UserRoleDetails_12102021063206.txt',
'UserRoleDetails_12102021064706 (1).txt',
'UserRoleDetails_12102021064706.txt',
'UserRoleDetails_12102021070206.txt']
array.forEach(function(currentItem){
readTextFile(currentItem);
})
The problem i am facing is that all files are processed at the same time and all the datas of all files are stored together.
first, this node js is not work in sequential as you mention here
and second, array.forEach is not useful here to do the sequential operation
you need to use
const { readTextFile } = require("./textReader");
var array=['UserRoleDetails_12102021063206.txt',
'UserRoleDetails_12102021064706 (1).txt',
'UserRoleDetails_12102021064706.txt',
'UserRoleDetails_12102021070206.txt']
for (const element of array) {
readTextFile(currentItem);
}
NOTE:- readTextFile(currentItem) your this function is not async so maybe you need to make it async
if you are not clear then raise your hand
I have the following code it should compare the values of a csv file with the values in the browser. The Values in the browser are stored in a Table.
var testarray = [];
var text = fs.readFileSync('..\...\fileName.csv');
rd.on('line',function(line) {
var x = 1;
})
.on('close', function(line) {
close csv
});
if (x = 1 ){
rd.on('line',function(line) {
var x = 1;
})
.on('close', function(line) {
close csv
});
}
var i;
while (i <|= testarray.length) {
browser.expect.element('Column name of value').which.contains.text('index of the current array')
i + 1;
}
hello I'm working on a project to store images in firebase and then get the url of each image and store it in a database (firebase) the problem that I can not store those urls in the same registry would like to know if I can create an array with the urls of the images that I upload from a multiple input file and save them in a single record in this way.
parques:{
nombre: "xxxxx",
imgs:[
"urls"
]
}
Here I leave the code:
var files = document.getElementById('file');
var fullDirectory= 'imgs';
var btnDatos= $('#btn-datos');
var btnImgs = $('#btn-imgs');
//proceso imgs
files.addEventListener('change', function(e){
for (var i = 0; i < e.target.files.length; i++) {
var imageFile = e.target.files[i];
Imagencollage(imageFile);
}
})
//subir imagenes
function Imagencollage(imageFile){
var links= [];
btnImgs.click(function (){
var storageRef = firebase.storage().ref(fullDirectory+"/"+imageFile.name);
var task = storageRef.put(imageFile);
var name = imageFile.name;
task.on('state_changed', function(snapshot){
//??
}, function(error) {
alert('no se subio el archivo');
}, function() {
task.snapshot.ref.getDownloadURL().then(function(downloadURL) {
links.push(downloadURL);
direccionImg(name, links);
});
});
})
}
//subir datos
function direccionImg(name, links){
btnDatos.click(function(){
var db= firebase.database().ref('parques/');
var parque= $('#datos').val();
var data= {
nombre: parque,
imgs: links
}
db.push().set(data)
})
}
In front of you, thank you for the help
I am trying to get data out from query inside the query. I tried everything without success.
getvideos() {
var sectionHolder = [];
var ids = [];
var titles = [];
var videos = [];
var challengeCl = Parse.Object.extend("Challenges");
var query = new Parse.Query(challengeCl);
query.find().then((data) => {
for(var key = 0; key < data.length; key++) {
var object = data[key];
var videoid = object.id;
var title = object.get('title');
ids.push(videoid)
titles.push(title)
var video = Parse.Object.extend('Videos');
var queryv = new Parse.Query(video);
queryv.equalTo('challengeid', videoid);
queryv.find().then(
(video) => {
videos.push(video); //i want to get this data out from this scope
});
sectionHolder.push({title: title, data: videos}); // <---- i want to put videos data in data
}
console.log(sectionHolder);
this.setState({listData: sectionHolder});
});
}
I read parse documentation but there is nothing related to this.
Let's say I want to upload 2 images to an ajax, I will send them using this format
{ "base64StringName:" : "[ {"1": "base64_1"}, {"2" : "base64_2"} ]"}
So its an object that contains an array of objects of base64 strings
To do so, I will need to create an array and inside this array, I will push json objects into it.
Here is my code for this:
<script>
var test ='';
var imageArray =[];
var imageObject ={};
$('#inputFileToLoad').on('change', function(){
imageArray.length = 0;
fileCount = this.files.length;
for(i = 0; i < fileCount; i++){
var file = document.querySelector('#inputFileToLoad').files[i];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
test = reader.result.split(',')[1];
console.log(test);
imageObject[i] = test;
imageArray.push(imageObject);
};
reader.onerror = function (error) {
alert('Error: ', error);
};
}
});
$('#inputFileToLoadButton').on('click', function(){
console.log(imageArray);
$.ajax({
url:"some url",
method:"POST",
data: {
"base64String": imageArray
}
,success: function () {
swal("Success!","Upload Finished!","success");
//add redirect!
},
error: function (jqXHR) {
swal("Error",jqXHR.responseText, "error");
}
});
});
</script>
However, I encounter a problem, my first object inside the array somehow gets overwritten.
it becomes
{ "base64StringName:" : "[ {"1": "base64_2"}, {"2" : "base64_2"} ]"}
Also when i printed out the first base64 encoded file at console.log(test); it is undefined, but when i printed out the second base64 encoded file, it prints the second file only.
try this:
var test = '';
var imageArray = [];
var imageObject;
$('#inputFileToLoad').on('change', function() {
imageArray.length = 0;
fileCount = this.files.length;
for (i = 0; i < fileCount; i++) {
debugger;
var file = document.querySelector('#inputFileToLoad').files[i];
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function() {
test = this.result.split(',')[1];
imageObject = {};
imageObject[index] = test;
imageArray.push(imageObject);
}.bind({
index: i
});
reader.onerror = function(error) {
alert('Error: ', error);
};
}
});