Using the secure Cordova plugin generates an “Unknown provider” error, why?

I am trying to use secure storage ( https://ionicframework.com/docs/v2/native/secure-storage/ ) in an Ionic app.

In my controller:

.controller('ExampleCtrl', function ($scope, SecureStorage) { var ss = new SecureStorage( function () { console.log('Success') }, function (error) { console.log('Error ' + error); }, 'my_app'); var key = 'aaa'; 

ERROR:

ionic.bundle.js: 26799 Error: [$ injector: unpr] Unknown provider: SecureStorageProvider <- SecureStorage <- ExampleCtrl

But he does not know the provider of SecureStorage.

Do you know what I am doing wrong?

+5
source share
2 answers

Please try the following:

 .controller('ExampleCtrl', function ($scope) { var ss = new cordova.plugins.SecureStorage( function () { console.log('Success'); }, function (error) { console.log('Error ' + error); }, 'my_app'); }); 

So, remove SecureStorage from the controller arguments and add cordova.plugins. in the second line.

And don't forget to install the plugin (of course):

 ionic plugin add cordova-plugin-secure-storage --save 

Now you can use the functions on ss , but do not use the documentation in the link you specified, as this is the Ionic 2 documentation. Instead, use the plugin documentation: https://github.com/Crypho/cordova-plugin-secure-storage

+1
source

I think you should wrap SecureStorage in the service, and then use the service in the controller.

 import { SecureStorage } from 'ionic-native'; .factory('secureFactory',function(){ this.createStorage = function(){ var ss = new SecureStorage( function () { console.log('Success') }, function (error) { console.log('Error ' + error); }, 'my_app'); return ss; } }) 

in the controller

  .controller('ExampleCtrl', function ($scope, secureFactory) { var sss = secureFactory.createStorage(); }) 
0
source

Source: https://habr.com/ru/post/1261392/


All Articles