Tosps
Tosps

Reputation: 83

Javascript append file with variable

this code below will successfully write "apple" into the text file called save.txt

//Write Text File
var total = $gameActors.actor(1).name();
var path = window.location.pathname.replace(/(\/www|)\/[^\/]*$/, '/');
if (path.match(/^\/([A-Z]\:)/)) {
      path = path.slice(1);
}
path = decodeURIComponent(path) + "save.txt";
var fs = require('fs');
fs.appendFile('save.txt',"apple", function(err) {
    if (err) throw err;
    console.log('created]');
});

I want to write the variable "total" into the file

I have tried

fs.appendFile('save.txt',total,
fs.appendFile('save.txt',var total,
fs.appendFile('save.txt',(total),

but none works. Any help is appreciated Thanks in advance

Upvotes: 1

Views: 1221

Answers (1)

Maidiries
Maidiries

Reputation: 169

Try this:

var fs = require('fs');

var total = $gameActors.actor(1).name();
if( !total ){ total = 0; }

fs.appendFile('save.txt', total, function(err) {
    if (err) throw err;
    console.log('created');
});

Upvotes: 1

Related Questions