最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - fs.writeFile not overwriting file - Stack Overflow

programmeradmin5浏览0评论

I am using node-crontab to run the script. fs.writeFile overwrite first time the loop runs but after that is appending data. I have tried deleting the file before writing but is doing the same, deletes it the first time but in the subsequent runs start to append. What should I do?

This is the script: I ommited some environment variables...

var jobId = crontab.scheduleJob('* * * * *', function() {
//Gettting  system date and adding leading zeros when are single digits.  I need this to build the get request with date filters.

    var d = new Date();
    var nday = d.getDate();
    var nmonth = d.getMonth();
    var nhour = d.getHours();
    var nmin = d.getMinutes();

    var nfullyear = d.getFullYear();
    if (nday < 10) {
        nday = '0' + nday;
    };
    var nmin = nmin - 1;
    if (nmin < 10) {
        nmin = '0' + nmin;
    };
    if (nhour < 10) {
        nhour = '0' + nhour;
    };

    var nmonth = nmonth + 1;
    if (nmonth < 10) {
        nmonth = '0' + nmonth;
    };

    var options = {
        url: 'https://[email protected]/v1/gpsdata' + '?fromdate=' + nfullyear + '-' + nmonth + '-' + nday + 'T' + nhour + '%3a' + nmin + '%3a' + '00',
        method: 'GET',
        rejectUnauthorized: !debug
    };


// HTTP get request
      request(options, function(error, response, body) {
        if (error) throw new Error(error);
        var result = JSON.parse(body)['gps-recs'];

        console.log(result.length);

        //create .csv file 
        buildCSV(result);

    });
});

function buildCSV(result) {


    //adding headers
    csvFile = csvFile.concat('UserNumber' + ',' + 'UserTimeTag' + ',' + 'Latitude' + ',' + 'Longitude' + ',' + 'SpeedMph' + ',' + 'Heading' + ',' + 'Status' + '\r\n');
    // loop runs result.length times
    for (var i = 0; i < result.length; i++) {
        csvFile = csvFile.concat(result[i].UserInfo.UserNumber + ',' + result[i].UserTimeTag + ',' + result[i].Latitude + ',' + result[i].Longitude + ',' + result[i].SpeedMph + ',' + result[i].Heading + ',' + result[i].Status + '\r\n');

    };
    //delete file.csv first
    console.log('before unlink: ');
    fs.unlink('file.csv', function(err){
        if (err) throw err; 
        else {
            console.log('file deleted'); 
            console.log(csvFile);
            fs.writeFile('file.csv', csvFile, function(err) {

            if (err) throw err;
            console.log('file saved');

            });
        };

    });
};

I am using node-crontab to run the script. fs.writeFile overwrite first time the loop runs but after that is appending data. I have tried deleting the file before writing but is doing the same, deletes it the first time but in the subsequent runs start to append. What should I do?

This is the script: I ommited some environment variables...

var jobId = crontab.scheduleJob('* * * * *', function() {
//Gettting  system date and adding leading zeros when are single digits.  I need this to build the get request with date filters.

    var d = new Date();
    var nday = d.getDate();
    var nmonth = d.getMonth();
    var nhour = d.getHours();
    var nmin = d.getMinutes();

    var nfullyear = d.getFullYear();
    if (nday < 10) {
        nday = '0' + nday;
    };
    var nmin = nmin - 1;
    if (nmin < 10) {
        nmin = '0' + nmin;
    };
    if (nhour < 10) {
        nhour = '0' + nhour;
    };

    var nmonth = nmonth + 1;
    if (nmonth < 10) {
        nmonth = '0' + nmonth;
    };

    var options = {
        url: 'https://[email protected]/v1/gpsdata' + '?fromdate=' + nfullyear + '-' + nmonth + '-' + nday + 'T' + nhour + '%3a' + nmin + '%3a' + '00',
        method: 'GET',
        rejectUnauthorized: !debug
    };


// HTTP get request
      request(options, function(error, response, body) {
        if (error) throw new Error(error);
        var result = JSON.parse(body)['gps-recs'];

        console.log(result.length);

        //create .csv file 
        buildCSV(result);

    });
});

function buildCSV(result) {


    //adding headers
    csvFile = csvFile.concat('UserNumber' + ',' + 'UserTimeTag' + ',' + 'Latitude' + ',' + 'Longitude' + ',' + 'SpeedMph' + ',' + 'Heading' + ',' + 'Status' + '\r\n');
    // loop runs result.length times
    for (var i = 0; i < result.length; i++) {
        csvFile = csvFile.concat(result[i].UserInfo.UserNumber + ',' + result[i].UserTimeTag + ',' + result[i].Latitude + ',' + result[i].Longitude + ',' + result[i].SpeedMph + ',' + result[i].Heading + ',' + result[i].Status + '\r\n');

    };
    //delete file.csv first
    console.log('before unlink: ');
    fs.unlink('file.csv', function(err){
        if (err) throw err; 
        else {
            console.log('file deleted'); 
            console.log(csvFile);
            fs.writeFile('file.csv', csvFile, function(err) {

            if (err) throw err;
            console.log('file saved');

            });
        };

    });
};
Share Improve this question asked Nov 12, 2015 at 18:06 Vanessa TorresVanessa Torres 1691 gold badge2 silver badges9 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 21

For one thing when I run your code I get an error if no file exists in the first place. See fix below.

To be sure you are writing you could can explicitly supply the write flag to the options parameter like so:

console.log('before unlink: ');
fs.unlink('file.csv', function(err){

    // Ignore error if no file already exists
    if (err && err.code !== 'ENOENT')
        throw err;

    var options = { flag : 'w' };
    fs.writeFile('file.csv', csvFile, options, function(err) {
        if (err) throw err;
        console.log('file saved');
    });
});

Btw, fs.unlink() is not really needed since fs.writeFile() overwrites the file.

发布评论

评论列表(0)

  1. 暂无评论