[Solved] Problem to store information

Posted on
  • Hi,

    I do not know why the JSON is not saved.

    I am setting a variable

    var settings = {
      hours : 0,
      minutes : 0,
      seconds : 0,
      started : false,
      counter : 0
    };
    

    Then I store this to a JSON file

    const storage = require('Storage'); 
    storage.writeJSON('test.json', settings); 
    

    Then I change the variable

    settings.counter = 12345;
    

    Then I store again

    storage.writeJSON('test.json', settings);
    

    And reload

    settings2 = storage.readJSON('test.json',1);
    

    Why is the counter (settings2.counter) not stored? It is 0 after reload, not 12345.

    This is the full code.

    var settings = {
      hours : 0,
      minutes : 0,
      seconds : 0,
      started : false,
      counter : 0
    };
    
    const storage = require('Storage');
    
    storage.writeJSON('test.json', settings);
    settings.counter = 12345;
    print(settings.counter); //=12345
    storage.writeJSON('test.json', settings);
    
    settings2 = storage.readJSON('test.json',1);
    print(settings2.counter); //=0, why is the counter not saved?
    

    Thanks
    Christian

  • Works fine for me. Though I had to use settings1 as variable name because settings is already reserved for the settings of my watch face.

    Maybe it is something similar for you? Is the settings variable already set to something before your code run?

    EDIT: have you checked how many test.json files you have? Maybe it is related to this post?

  • I think it may be a mix of both.
    I changed the settings variable in my app and widget and now saving works.
    But at one time during testing, I really had two test.json files on the watch.

  • Good to hear you got it working. Can you reproduce how it came to the two test.json files? Might be helpful for @Gordon to find a fix.

  • Can you reproduce how it came to the two test.json files

    Never happened again...

  • Can you reproduce how it came to the two test.json files?

    Yes, if you ever can it'd be amazingly helpful in trying to find a fix!

  • So I could reproduce writing duplicate files with a mixture of writeJSON and write
    Not sure it is what happened here and with the alarm app though, but maybe it is related:

    let set={"id":1,"test":2};
    let sto=require("Storage");
    sto.writeJSON("test.json",set);
    sto.write("test.json","test123");
    sto.list("test.json");
    =[
      "test.json",
      "test.json"
     ]
    >sto.read("test.json")
    ="test123"
    >sto.readJSON("test.json")
    Uncaught SyntaxError: Expecting a valid value, got ID
     at line 1 col 8
    test123
           ^
    

    So both readand readJSON then only can read the latter file.

  • mixture of writeJSON and write

    My code used this too.
    I changed everything to writeJSON yesterday.
    That could really be the culprit.

  • Thanks for this! Although what firmware are you using? I just tried this on the KickStarter firmware and latest, and I get:

    let set={"id":1,"test":2};
    let sto=require("Storage");
    sto.writeJSON("test.json",set);
    sto.write("test.json","test123");
    sto.list("test.json");
    =[
      "test.json"
     ]
    

    So it doesn't seem to reproduce it for me :(

  • VERSION: "2v05",
    GIT_COMMIT: "09aa0d84",
    

    Strange indeed. I just tested it again with the same code and it did not work this time.

  • I was 100% sure yesterday, that my inital code worked before (stored the info to memory).
    Then it stopped working, why I opened this thread.
    With another app (Chrono Widget) I had to delete the JSON file from time to time or I would get strange behavior. Maybe something corrupt in the file or file system?
    Loading the file it looked ok though.

  • Thanks everyone! Got it!

    Wow, that was painful.

    s=require("Storage");
    s.eraseAll();
    s.write("a",new Uint8Array(4000));
    s.write("b",new Uint8Array(100));
    s.write("b",new Uint8Array(10));
    

    Issue filed at https://github.com/espruino/Espruino/issues/1801 with a bit of explanation too

  • All fixed - if you install firmware from http://www.espruino.com/binaries/travis/master/

  • Yup... Can't reproduce the Alarms issue on v2.05.440 anymore. Nice!

  • Wohoo!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

[Solved] Problem to store information

Posted by Avatar for Purple-Tentacle @Purple-Tentacle

Actions