Now I show the information using:
console.log (kraken.id, markets)
However, I want to write all the information that goes to the console to a file instead. How can that be done by completing the below code?
'use strict';
var ccxt = require('ccxt');
(async () => {
let kraken = new ccxt.kraken()
let markets = await kraken.load_markets()
//console.log (kraken.id, markets)
//How to write above console.log to file?
const fs = require('fs');
fs.writeFile("/Users/Andreas/Desktop/NODE/myproject/files/test.txt", "allinfoAsstring", function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
})()
In Node, console. log() calls util. inspect() to print objects. You should call that directly and write it to a file.
warn() Probably the most obvious direct replacement for log() , you can just use console. warn() in exactly the same way.
The console. log() method in HTML is used for writing a message in the console. It indicates an important message during testing of any program.
You can try to create an Object out of your variables and format them as a JSON string.
/* ... */
const obj = {kraken, markets}
const fs = require('fs');
fs.writeFile("/Users/Andreas/Desktop/NODE/myproject/files/test.txt", JSON.stringify(obj), function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
Later, you can retrieve the values from the file, by running
fs.readFile('/Users/Andreas/Desktop/NODE/myproject/files/test.txt', 'utf8', function(err, data) {
const obj = JSON.parse(data)
console.log("The data from the file is: " + obj)
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With