dilpesh jain dilpesh jain - 3 months ago 17
Javascript Question

How to receive error while inserting in store?

I am using angular-indexedDB for indexedDB in AngularJS.
I want to receive error if insert is not successful. But I am not getting any error it just comes out of the function if I run the same code twice as I have made name unique.

Error:


ConstraintError return _this.store.add(item);

ConstraintError req =
this.store.openCursor();


Code:

angular.module('myModuleName')
.controller('myControllerName', function($scope, $indexedDB) {

$scope.objects = [];

$indexedDB.openStore('people', function(store){

store.insert({"ssn": "444-444-222-111","name": "John Doe", "age": 57}).then(function(e){console.log('inside insert');});

store.getAll().then(function(people) {
// Update scope
$scope.objects = people;
});
});
});

Answer

I think adding error callback inside promise .then should work

Also do create a getAll method to retrieve all data from objects, in that do response.data to get data returned from server.

Code

angular.module('myModuleName')
  .controller('myControllerName', function($scope, $indexedDB) {

    $scope.objects = [];

    $scope.getAll = function(){
       store.getAll().then(function(response) {  
          // Update scope
          $scope.objects = response.data;
       });
    };
    $indexedDB.openStore('people', function(store){

       store.insert({"ssn": "444-444-222-111","name": "John Doe", "age": 57})
       .then(function(e){
           console.log('inside insert');
           //reload data only call get succeed
           $scope.getAll();
        }, function(error){
           //do error handling stuff here
           //you will get error returned from server here.
           console.log('Error here', error)
        });

    });
});
Comments