如何在存储中插入时接收错误

How to receive error while inserting in store?

本文关键字:错误 插入 存储      更新时间:2023-09-26

我在AngularJS中使用angular indexedDB作为indexedDB。如果插入不成功,我希望收到错误。但我并没有得到任何错误,如果我运行两次相同的代码,它只是来自函数,因为我已经使名称唯一。

错误

ConstraintError return _this.store.add(item);

约束错误请求=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 .then中添加错误回调应该可以使用

还要创建一个getAll方法来从objects检索所有数据,在该方法中执行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)
        });
    });
});