Pricing Blog

Local Storage Key Stuck as Null After Deletion in Toddle

  • lunar_rei-1376927122496557127

    Rei

    6 months ago

    I’m building a “recent items” feature in Nordcraft that stores an array in local storage under the key kb-recent-items. The setup uses the formula Default to(Get from Local Storage("kb-recent-items"), []) to initialize the variable, so it should always be an array—even if the key is missing or deleted (Get from Local Storage).

    Initially, the feature worked without issue.
    However, for testing purposes, I opened up my browser's dev tools and deleted the storage key.
    On page reload, the key is recreated but its value is null, and my workflow (which sets the variable and saves it back to local storage) does not update it. I’ve tried triggering the update both on load and on attribute change (Lifecycle section of the data panel), but the issue persists. The UI either shows null or [object] instead of the expected array of recent items.

    What I’ve Tried:

    -Initializing the variable with Default to(Get from Local Storage("kb-recent-items"), [])
    -Triggering the update workflow on both load and attribute change
    -Ensuring the workflow sets the variable and saves it to local storage

    Issue:
    After deleting the key, it comes back as null and will not update, even though the workflow should be setting and saving the array. Has anyone else run into this, or does anyone know what might be causing the key to get stuck as null and not update as expected?

    Any advice or workarounds would be appreciated!
  • lucasg-1376936153361416233

    Lucas G

    6 months ago

    What you’re seeing is likely not type null but just a null string, in other words, the word null is your value
  • This will always be true and won’t default to []
  • Reason is that local storage, cookies, etc only accept string values
  • So if you want to save objects, do so with JSON strings
  • lunar_rei-1377251054428622971

    Rei

    6 months ago

    Thank you so much @Lucas G for the tip!
    It turns out you were 100% correct. 👍

    Assistant Context
    When you use local storage (or session storage), all values are stored as strings. If you save the value null directly, it will be stored as the string "null", not as a true null value or an empty array. When you later retrieve this value, you get the string "null", which will not be treated as a real null or an array by your formulas. This is why your defaultTo formula does not fall back to []—because the value is not actually null, but the string "null".

    Resolution
    The workflow tried to add an item to a non-existent array, causing the local storage key to be set with the string "null".
    I expected the defaultTo function to fall back to an empty array since I used double brackets [], but it didn’t.
    After some troubleshooting, I discovered the brackets were being treated as the string "[]" rather than an actual empty array type.

    To fix this, I removed the brackets and connected an Array function instead, which completely resolved the issue.

    Thanks again LucasG as I wouldn't have been able to figure this out without you pointing me in the right direction. ☺️
    👍1
  • Tod-1377251056773501020

    Tod

    6 months ago

    Great job @Rei! Your contribution to the Nordcraft Community just made you advance to Community Level 2! 🌲
  • whitep4nth3r-1377290891521167381

    salma

    6 months ago

    I have made similar mistakes in the past @Rei

    When you're used to setting an empty array in code as [] it's easily missed 😅

    I still do this with empty strings all the time 😄