0

I am trying to have a node js script write some coordinates to a csv file for use in a Newman CLI script. I have the following:

const axios = require('axios');
var move_decimal = require('move-decimal-point');
var sLat = 45.029830;
var sLon = -93.400891;
var eLat = 45.069523;
var eLon = -94.286001;
var arrLatLon = []

axios.get('http://router.project-osrm.org/route/v1/driving/' + sLon + ',' + sLat + ';' + eLon + ',' + eLat + '?steps=true')
.then(function (response) {
    for (let i = 0; i < response.data.routes[0].legs.length; i++) {
        //console.log(response.data)
        for (let ii = 0; ii < response.data.routes[0].legs[i].steps.length; ii++) {
            //console.log('leg ' + i + " - step " + ii + ": " + response.data.routes[0].legs[i].steps[ii].maneuver.location[1] + "," + response.data.routes[0].legs[i].steps[ii].maneuver.location[0]);

            // Declaring Latitude as 'n' & Longitude as 'nn' for decimal calculations
            var n = response.data.routes[0].legs[i].steps[ii].maneuver.location[1]
            var nn = response.data.routes[0].legs[i].steps[ii].maneuver.location[0]

            // Latitude calculatiuons to make 'lat' values API friendly
            var y = move_decimal(n, 6)
            var p = Math.trunc(y);

            // Longitude calculations to make 'lon' values API friendly
            var yy = move_decimal(nn, 6)
            var pp = Math.trunc(yy);

            arrLatLon.push(p +  "," + pp);
        }
        console.log(arrLatLon)
    }
})

I have been looking through and trying numerous different tutorials/code snippets regarding writing the array elements from arrLatLon to an output file on my local machine, but none have been successful. The current code outputs the lat,lon correctly, console.log(arrLatLon) outputs:

[ '45029830,-93400894',
  '44982812,-93400740',
  '44977444,-93400530',
  '44973116,-93410884',
  '44971101,-93450400',
  '45035514,-93766885',
  '45035610,-93766886',
  '45081631,-94286752',
  '45070849,-94282026' ]

any help would be greatly appreciated. Thanks.

ClintT
  • 3
  • 1
  • 2
  • Look into the node fs module... https://nodejs.org/dist/latest-v10.x/docs/api/fs.html#fs_fs_writefile_file_data_options_callback – SakoBu Dec 27 '18 at 22:00
  • Oh just saw the node.js tag...whoops. Not sure why you're using axios server-side. – Patrick Roberts Dec 27 '18 at 22:00
  • @PatrickRoberts This is running on a client-side machine not the server-side – ClintT Dec 27 '18 at 22:02
  • Let me rephrase: I'm not sure why you're using axios _in Node.js_. – Patrick Roberts Dec 27 '18 at 22:03
  • @SakoBu I have tried different variations of fs.createFileStream and fs.writeFile(Sync) etc and the file is empty every time. It's getting "updated" as np++ alerts saying the file has changed; however, its still blank. – ClintT Dec 27 '18 at 22:05
  • @PatrickRoberts Noted, but axios is not the issue. – ClintT Dec 27 '18 at 22:07
  • @ClintT might want to make sure your node process has the proper permissions to write a file. _And that you're performing the `fs` write in the callback of the axios request._ – Patrick Roberts Dec 27 '18 at 22:10

1 Answers1

1

With nodejs you can easily write files using the fs module

const fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
}); 

in your case you can simply do something like

const fs = require('fs');
// I'm converting your array in a string on which every value is
// separated by a new line character
const output = arrLatLon.join("\n");

// write the output at /tmp/test
fs.writeFile("/tmp/test", output, function(err) {
    if(err) {
        return console.log(err);
    }

    console.log("The file was saved!");
}); 

Let me forward you to this question for more information Writing files in Node.js

Michael
  • 792
  • 1
  • 7
  • 14