编程语言
首页 > 编程语言> > javascript-插入商店时如何接收错误?

javascript-插入商店时如何接收错误?

作者:互联网

我在AngularJS中将angular-indexedDB用于indexedDB.
如果插入不成功,我想接收错误.但是我没有收到任何错误,如果我两次运行相同的代码(使名称唯一),它只是从函数中产生的.

错误:

ConstraintError return _this.store.add(item);

ConstraintError req =
this.store.openCursor();

码:

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;
      });
    });
});

解决方法:

我认为在promise中添加错误回调.然后应该工作

还要创建一个getAll方法来从对象检索所有数据,在该方法中,也可以使用response.data来获取从服务器返回的数据.

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)
        });

    });
});

标签:indexeddb,angularjs,angularjs-directive,javascript
来源: https://codeday.me/bug/20191118/2031596.html