Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NativeStorage is unavailable/undefined #10

Closed
marianfeiler opened this issue Apr 21, 2016 · 16 comments
Closed

NativeStorage is unavailable/undefined #10

marianfeiler opened this issue Apr 21, 2016 · 16 comments

Comments

@marianfeiler
Copy link

marianfeiler commented Apr 21, 2016

I installed the Plugin, but it is not working.
nativeStorage is not available.

I try on deviceready:

var ns = (typeof nativeStorage == "undefined") ? false : true;
if(ns)
{
    alert("nativeStorage is available");
    nativeStorage.putString(
        "foo",
        "bar",
        function(result)
        {
            alert(result);
         },
         function(e)
         {
            alert(e);
         }
    );
    nativeStorage.getString(
        "bla",
        function(result)
        {
            alert(result);
        },
        function(e)
        {
            alert(e);
        }
    );
}

any ideas? I am using phonegap actual

@alokrajiv
Copy link
Collaborator

alokrajiv commented Apr 21, 2016

could you specify the platform you are using Android/iOS and the version.

@marianfeiler
Copy link
Author

I am working on iOS 9.3.1

@alokrajiv
Copy link
Collaborator

I am able to run it on iOS v9.3 without any issues.
could you run
cordova plugin ls
and confirm output of the above command to have:
cordova-plugin-nativestorage 1.0.9 "NativeStorage".

@marianfeiler
Copy link
Author

I confirm the output, see below:

iMac:mySprit marianfeiler$ sudo cordova plugin ls 
cordova-plugin-console 1.0.2 "Console"
cordova-plugin-nativestorage 1.0.9 "NativeStorage"
cordova-plugin-statusbar 1.0.1 "StatusBar"

@alokrajiv
Copy link
Collaborator

@marianfeiler
I noticed you are using typeof nativeStorage
could you try with typeof NativeStorage with the first letter 'n' as capital 'N'.

@marianfeiler
Copy link
Author

I tried, but i run in Error.
But I tried and tried, and then, I think it was Build Error with Phonegap.
Now it works like a charm :)
But I think the handling could be nicer... like just so for setting:
var result = nativeStorage.set("foo", "bar"); // returns true|false
or for getting:
var myString = nativeStorage.getString("foo"); // returns String (bar) and in error false
Thanks again a lot for helping...

@alokrajiv
Copy link
Collaborator

Great to know you got it working. So, thank you for confirming. I am closing this issue.
Also we don't have a return type, because the operation is asynchronous.
So, for errors as-per Javascript standards, you could use the 4rd parameter of NativeStorage.set() to pass an error handler function, which will be called with the error as parameter.

@GillesC
Copy link
Collaborator

GillesC commented Apr 21, 2016

I've opted for callback functions because of the nature of JavaScript. It's an inherent strategy used in JavaScript. It makes asynchronuous communication possible. Thus, not blocking the rest of the code. The overhead introduced by Cordova is in that manner less obvious.

@Neo1975
Copy link

Neo1975 commented May 27, 2016

Hi,
I had the same problem. After many test try and debugging I understand that: angular application, service and controller are initialized before cordova plugins. So in my case I use NativeStorage object at Service startup.
To avoid problems I suggest you to:

  • Remove ng-app from body
  • put this cose at the end of index.html

<script type="text/javascript"> document.addEventListener('deviceready', function onDeviceReady() { angular.bootstrap(document.querySelector('body'), ['starter']); }, false); </script>

Instead I would like to advice @GillesC to insert this instructions inside Readme file.

@GillesC
Copy link
Collaborator

GillesC commented May 27, 2016

@Neo1975 I've written a wrapper, can u see if the problem persists when using the wrapper to access the NativeStorage functionalities. If so I will include your instructions for this problem in the README, otherwise the users should use the wrapper for using the plugin in Ionic or other AngulerJS related frameworks.

Edit: the wrapper only wraps the functionalities provided as of v2.

@GillesC GillesC changed the title not working NativeStorage is unavailable/undefinied May 27, 2016
@GillesC GillesC changed the title NativeStorage is unavailable/undefinied NativeStorage is unavailable/undefined May 27, 2016
@Neo1975
Copy link

Neo1975 commented May 27, 2016

Indeed, I imported wrapper project as you describe in the ReadMe file: the
exception is throw by getItem and setItem method found NativeStorage
undefined. Perhaps can depends on Ionic Framework but in the way that I
describe above now works well.

Il giorno ven 27 mag 2016 alle ore 18:04 Gilles Callebaut <
[email protected]> ha scritto:

@Neo1975 https://github.com/Neo1975 I've written a wrapper
https://github.com/TheCocoaProject/ngcordova-wrapper-nativestorage, can
u see if the problem persists when using the wrapper to acces the
NativeStorage functionalities. I so I will include your instructions for
this problem in the README, otherwise the users should use the wrapper for
using the plugin in Ionic or other AngulerJS related frameworks.


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#10 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AA9iQFhb0Bx12lR647B-D7bU2bDoRm36ks5qFxYfgaJpZM4IMl75
.

@GillesC
Copy link
Collaborator

GillesC commented May 27, 2016

@Neo1975 Thanks for your contribution, I will update the readme and will look further into the matter, when I have more time.

GillesC added a commit that referenced this issue May 27, 2016
@jwuliger
Copy link

Hello. I know this is closed, but I am trying to use the fix described by @Neo1975. I am not able to get my angular app to run. Is there a more specific place that I place the javascript bootstrapper after I remove ng-app from my body tag? Thanks so much for any help.

@alokrajiv
Copy link
Collaborator

hi @jwuliger, not very familiar with the exact issue, but did you try to bootstrap after onready using the script as mentioned by @Neo1975.

@jwuliger
Copy link

Hello @alokrajiv Thanks for the reply. Yes, I am using the script as suggested, just changed the app name. I have tried placing it in many different locations at the bottom of my index.html file. Here is a snippet:

    /// all my angular/ionic & app js is above ///
    <script src="lib/ngcordova-wrapper-nativestorage/dist/ngcordova-wrapper-nativestorage.min.js"></script>
</head>

<body> <!--ng-app="app"-->
    <ion-nav-view></ion-nav-view>
</body>
    <script type="text/javascript">
        document.addEventListener("deviceready", function onDeviceReady() {
            angular.bootstrap(document.querySelector("body"), ["app"]);
        }, false);
    </script>
</html>

I suppose this was the solution to getting "undefined" when using a get call when there is nothing in storage yet. Like I said I have tried many iterations of the code placement. Maybe I something else in the wrong place. Thanks for your help!

@alokrajiv
Copy link
Collaborator

@jwuliger Which version of angular are you using? also are you using ionic? without ionic I am able to inject $cordovaNativeStorage without any troubles using ng-app itself, no bootstrapping.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants