function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Gonzalo Huerta CánepaGonzalo Huerta Cánepa 

Storage service access from a component

I am trying to use the storage service in my app.
What I did was to create an app template that has the following:
<aura:set attribute="auraPreInitBlock">
        <!-- Note that the maxSize attribute in <auraStorage:init> is in KB -->
        <auraStorage:init name="mystorage"
                          maxSize="4096"
                          clearStorageOnInit="false"
                          defaultExpiration="3600"
                          debugLoggingEnabled="true"/>
    </aura:set>
Then my app uses that as template.
One of the component of the app is trying to access the storage using $A.storageService.getStorage("myStorage"); but I get an error saying that $A.storageService is undefined.
I put a console.log and indeed it is undefined, but if I open a console in the same page (Using developer tools in chrome) and I execute console.log($A.storageService) then it works OK.
So my guess is that the scope of the $A variable changes btween the app and each component.
Any ideas?
 
Best Answer chosen by Gonzalo Huerta Cánepa
Mark SFMark SF
$A.storageService is not supported in Lightning components. It's only available in the underlying Aura open-source implementation currently.

You should only use the JavaScript API methods exposed and documented in the reference doc app. See https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/ref_doc_app.htm

Access to docuemnted methods hasn't been enforced inthe past. The LockerService feature, which is a critical update in Summer '16, will enforce that users only access documented methods. LockerService is scheduled to be enabled for all users in the Winter release.

From the lead dev:
Aura Storage Service's JavaScript API is not exposed in Lightning yet. But it's on the roadmap to expose as a general key-value store. Expect it in the next couple releases (safe harbor).