MV How to save text in file as a string to be put in message box?(Solved)

Hello!

How do I save the information within a textfile to a string?
Currently I am trying to use this:

JavaScript:
data = ""
fs.readFile("note.txt", 'utf8', (data) => {
    console.log(data)
    result = data;
});
console.log(result)

This sadly doesn't work well, primarly since I don't know what the "data" is, I just saw it somewhere and said wth and tried it without any expectations...

Regardless, in the same folder as index.html, there is a file named note.txt.
And in this note is some lines of text, and I would like to save this text to a string.
Then I want to display this string in a messagebox.

And I only need to know how to get the text into a string format. The rest I know 🙂
Does it have to be in a txt file? You can add a js file to your plugins list

myTextFile.js
var myText = 'This is the text that I want to display in a message box';


then to read out that string you just use $gameMessage.add(myText)
Does it have to be in a txt file? You can add a js file to your plugins list

myTextFile.js
var myText = 'This is the text that I want to display in a message box';


then to read out that string you just use $gameMessage.add(myText)
Indeed, however, the string in question would contain about 50-100 lines, and I'm looking to add about 10-15 of these text files. And that means I'd have to do:
JavaScript:
var string = "this is line 1" +
    "this is line 2" +
    "this is line 3" +
    "this is line 4" +
    etc etc
Alternatively I'd have to change the text .

What I am planning to do is essentially read the file and save it as a string. Then I will split the string into an array based on where the line breaks are. Then on each index I will append <br> to the end of the string.

This also means I could, if easier, loop each line in the document and simply add each line to an array.

Edit:
This turned out to be quite easy:

Code:
fs = require("fs");
string = fs.readFileSync("note.txt", "utf8");
strings = string.split(/\r?\n|\r/g);
You already found a solution, but let me explain why your first code didn't work.

JavaScript:
data = ""
fs.readFile("note.txt", 'utf8', (data) => {
    console.log(data)
    result = data;
});
console.log(result)

First, data = "" should be result = "";, because you're going to use result in console.log(result);, not data.

Next, you need to call const fs = require("fs"); before you can use fs.readFile(), because otherwise fs doesn't exist. By the way, fs is a module built in Node.js (what RPG Maker runs on), but you still need to require() it before you can use it. Other programming languages have something similar, such as import (Java, Python), using (C++, C#), include (PHP), etc.

Then, fs.readFile() is asynchronous. This means that your program will continue running while fs.readFile() is doing its job. Therefore, console.log(result) could execute before result = data; is set, and you would only get undefined in this case.

And finally, (data) => { /* code */ } should be (err, data) => { /* code */ }. According to the documentation for fs.readfile(path, options, callback) (https://nodejs.org/api/fs.html#fsreadfilepath-options-callback), callback is a function that takes 2 arguments: err (Error) and data (string). The arguments are given in that precise order, but you're only taking 1 argument, and ignoring the rest. In this case, you're taking the err argument (mistakenly calling it "data"), and ignoring the actual data.


Also, here's a "best practice" tip:

Don't declare and define variables without const, var, or let (const is for constants, which are like variables, except that you can't change their value afterwards). When you do that, you're essentially creating a global variable. This can conflict with other (global) variables elsewhere in your code, which can cause bugs that are extremely difficult to fix, because somewhere in your code there's a function that changes the value of your variable, and you don't know which function it is, or where it is.

If you really need a global variable, then use the window object. So instead of result = data, it's better to use window.result = data. The effect is the same, but when you use the window object, not only do you know you intended to use a global variable, but it's also easier to search for.
I see you already solved it, but you could also work with json files, here is the function to read a json file:
JavaScript:
  function readFile(file){
    let absolutePath = process.cwd();
    if(!absolutePath.includes('www')) absolutePath = absolutePath + '\\www';
    absolutePath = absolutePath + '\\' + file + '.json';
    if(fs.existsSync(absolutePath)){
      let rawData = fs.readFileSync(absolutePath);
      let jsonData = JSON.parse(rawData);
      return jsonData;
    }
  }

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/how-to-save-text-in-file-as-a-string-to-be-put-in-message-box-solved.143239/