Javascript 如何在存储区中插入时接收错误?
我在AngularJS中使用indexedDB。 如果插入不成功,我希望收到错误。但我没有得到任何错误,它只是从函数中出来,如果我运行相同的代码两次,因为我已经使名称唯一 错误:Javascript 如何在存储区中插入时接收错误?,javascript,angularjs,angularjs-directive,indexeddb,Javascript,Angularjs,Angularjs Directive,Indexeddb,我在AngularJS中使用indexedDB。 如果插入不成功,我希望收到错误。但我没有得到任何错误,它只是从函数中出来,如果我运行相同的代码两次,因为我已经使名称唯一 错误: angular.module('myModuleName') .controller('myControllerName', function($scope, $indexedDB) { $scope.objects = []; $indexedDB.openStore('people', fu
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;
});
});
});
返回_this.store.add(项目)
约束恐怖请求=
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方法,从对象
检索所有数据,在该方法中,doresponse.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)
});
});
});
谢谢Pankaj,现在我能够检测到错误,但它仍然来自函数。Error here=由于不满足约束,事务中的变异操作失败。例如,对象存储或索引等对象已存在,并且正在尝试创建新对象。返回_this.store.add(项目);constraintorreq=this.store.openCursor()@dilpeshjain您不是在服务器端处理了那个错误吗?我的目标是找出这个条目是否已经在数据库中,根据结果我想执行一些任务。@dilpeshjain所以数据库返回约束验证不是正确的吗?我想说..在调用insert方法之前,请在服务器端pass上创建一个新的方法
,并将此对象传递给它,并检查是否已经存在。。如果它存在,则返回一个错误,并在客户端显示..否则继续执行insert..这是正确的,但为什么它从函数中出来,我在同一函数中要执行一些其他任务。使用角度和索引的原因是为了在客户端执行大多数任务。