CSV in javascript

Hi,

I have a question about CSV in javacript. How to do data loggin with date and time and all readings can be seen in CSV.

#!/usr/bin/env node
const b = require('bonescript');
const scale = 1;    // Set to 1 for Black and 2 for AI
console.log("scale set to", scale);
setInterval(read, 5000);

function read(){
    b.analogRead('A5', printStatus);
}

function printStatus(err, value) {
    var analogVoltage = value*1.75; // ADC Value converted to voltage
    console.log('nuskaityta = ' + analogVoltage); 
    var slegis = scale * analogVoltage / 0.00699;
    console.log("slÄ—gis " + 
    parseFloat(slegis).toFixed(3) + " mBar");
    const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const csvWriter = createCsvWriter({
  path: '/home/debian/out.csv',
  header: [
    {id: 'matavimas', title: 'Matavimas'},
  ]
});

const data = [
  {
    matavimas: slegis
    
  }
];
csvWriter
  .writeRecords(data)
  .then(()=> console.log('The CSV file was written successfully'));
}

Hi @aiv, welcome to the forums

[off-topic]
When you post code in the forum, you need to format it. To do so you can either select all the code and click the </> button, or type 3 backticks ``` on a separate line both before and after the code block.

I have done it for you this time.
[/off-topic]

you mean you want to save a date format of the current time as a csv column?

as in:

header: [
    {
        id: 'matavimas', title: 'Matavimas',
        id: 'date', title: 'Date'
    }
  ]
});

const data = [
  {
    matavimas: slegis,
    date: Date()
  }
];

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.