Trying to write note to text file

This is an MV 1.3.0 problem.  I know basic programming but I'm fairly new to both RPG Maker and Javascript.


I'm trying to create a script that writes an object's note to a text file.  I started by grabbing this sample code from a tutorial by SumRndmDde:


//Write Text File
var path = window.location.pathname.replace(/(\/www|)\/[^\/]*$/, '/data/');
if (path.match(/^\/([A-Z]\🙂/)) { path = path.slice(1);
}
path = decodeURIComponent(path) + "save.txt";
var fs = require('fs');
fs.writeFile(path, "Game Complete", function(err) { if(err) { return console.log(err); }
});


This works as expected, creating (or overwriting) a "save.txt" file with the words "Game Complete" in it.  But step two - getting it to write a note instead - failed.  I've tried replacing "Game Complete" with both of the following:


$dataClasses[1].note
String($dataClasses[1].note)


...and all I get is an empty text file.  The note for class 1 is NOT empty.


Any idea what I'm doing wrong?
What is in the note?  There could be some characters that mess with the text/format and cause it to be empty.


I've done similar, but with a text string and not a note, and I use fs.writeFileSync instead of fs.writeFile and it works fine for me.
At first it was:


<Subclass: 5>


<Default Row: 1>


When it failed I added a couple of lines of garbage text to make sure it wasn't just the tags being ignored:


<Subclass: 5>


<Default Row: 1>


tsrhsrth


"swgr"


It still fails to write anything to file.


And I just tried substituting in fs.writeFileSync.  The game now crashes with a "TypeError: Bad arguments" message.
I only pass in the file/path and the text, no error handling.


Have your script log  $dataClasses[1].note to the console prior to the write command, just to make sure it contains what you think it contains.  If it prints out blank, then you know there's something going on with the database and it's not your script at all.
Actually, something weird just happened.  I realized I had cut the text from the notebox to paste it here instead of copying and copied it from here to paste it back in.  Now it's writing to file suddenly (I'm using fs.writeFile again).  I don't know why.


It's all printing out on a single line, though.  Newline characters apparently need not apply.
You might need to do a replace - newline in JS/json and in a text editor might not be the same.  And it might depend on what text editor you're viewing the output with.  You might need to replace \n with \n\r or something like that.  Experiment a little.
Thanks for the help.

Original thread: https://forums.rpgmakerweb.com/threads/trying-to-write-note-to-text-file.69897/