Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Edit string in text file

ANSWERED

this is my code 

var accelData = [];

if (Accelerometer) {
const accel = new Accelerometer({ frequency: 1 });
accel.addEventListener("reading", () => {
accelData.text = JSON.stringify({
x: accel.x ? accel.x.toFixed(1) : 0,
y: accel.y ? accel.y.toFixed(1) : 0,
z: accel.z ? accel.z.toFixed(1) : 0
});

});
sensors.push(accel);
accel.start();
} else {
accelLabel.style.display = "none";
accelData.style.display = "none";
}

 

var timer = setInterval(function(){
file = fs.openSync("utf8.txt" , "w+");
fs.writeSync(file , accelData.text) ;

let utf8_read = fs.readFileSync("utf8.txt", "utf-8");
console.log("UTF-8 Data: " + utf8_read);

fs.closeSync(file)

},5000)

 

I want is to save the sensor data in text every 5 seconds and see the size of the text file.

but ...

 

 
[오후 4:03:52]Unhandled exception: ReferenceError: file is not defined
[오후 4:03:57]Unhandled exception: Error: Couldn't open file: utf8.txt

 

If you use writeFileSync, it is said that if there is no file, it is created. Why is there no file?

 

And if you don't open and write the file like that, the text file will be created normally.

 

Could anyone please tell me this?

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

I'd recommend reading the filesystem guide here https://dev.fitbit.com/build/guides/file-system/

You were missing var, let or const here:

 

[오후 4:03:52]Unhandled exception: ReferenceError: file is not defined
let file = fs.openSync("utf8.txt" , "w+");

View best answer in original post

Best Answer
0 Votes
1 REPLY 1

I'd recommend reading the filesystem guide here https://dev.fitbit.com/build/guides/file-system/

You were missing var, let or const here:

 

[오후 4:03:52]Unhandled exception: ReferenceError: file is not defined
let file = fs.openSync("utf8.txt" , "w+");
Best Answer
0 Votes