05-12-2019 15:23
05-12-2019 15:23
Hi all,
I'm creating a simple counting app to keep track of the number of food cravings I've experienced in a day.
I've got it all working except the storage.
I'm looking for a simple way to save the value of a single variable, so it persists between app log-ins and log outs.
Has anyone a short tutorial or example of how to do this using json?
_________________
let cravings_logged = stored.value;
let number;
(if cravings_logged has a stored value) {
number = cravings_logged
} else {
number = 0;
}
I tap on a button and, each time I do,
number = number +1
.....
and when I go to log out I want to write to memory...
stored.value = number
...
any advice on how to do this?
Answered! Go to the Best Answer.
05-12-2019 20:47 - edited 05-12-2019 20:48
05-12-2019 20:47 - edited 05-12-2019 20:48
Fixed it! Kept getting an error thrown, so resolved it using this code -
var old_data;
try {
old_data = fs.readFileSync("cbor.txt", "cbor");
} catch(e) {
console.log("oh snap we have an:", e);
let new_data;
new_data = {
"cravings_now": 0
};
fs.writeFileSync("cbor.txt", new_data, "cbor")
}
05-12-2019 20:47 - edited 05-12-2019 20:48
05-12-2019 20:47 - edited 05-12-2019 20:48
Fixed it! Kept getting an error thrown, so resolved it using this code -
var old_data;
try {
old_data = fs.readFileSync("cbor.txt", "cbor");
} catch(e) {
console.log("oh snap we have an:", e);
let new_data;
new_data = {
"cravings_now": 0
};
fs.writeFileSync("cbor.txt", new_data, "cbor")
}