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

Finding a simple solution for storing a single variable

ANSWERED

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? 

 

Best Answer
0 Votes
1 BEST ANSWER

Accepted Solutions

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")

}

View best answer in original post

Best Answer
1 REPLY 1

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")

}

Best Answer