Can fs in node koa2 create js files?

there is no problem in creating a file in txt format, but when you create a file in js format, you will see that [object Object]
is written like this. IndustryReader is a [{}] Array (object) format

from other files.
    const industryReader = require("../reader.js")
    industryReader[0].reader = parseInt(industryReader[0].reader)+1
    fs.writeFile("reader.js",industryReader,function(err){
        if (!err){
            console.log("wrote data to file.txt")
        }else{
            throw err;
        }
    });
Mar.11,2021

industryReader toString () is called by default, which is changed to

.
fs.writeFile('reader.js',JSON.stringify(industryReader),function(err){
//...
}

to sum up, all the files introduced by fs are in string format, so the files introduced and written are all in string format. Take json as an example, after introduction, you need to call JSON.parse () to convert the string to json format. Before writing, you need to call JSON.stringify () to write the json into a string.

Menu