gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 10:36:01 25 4
gpt4 key购买 nike

我正在使用 angular-indexedDB对于 AngularJS 中的 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 .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)
});

});
});

关于javascript - 插入商店时如何收到错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36235584/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com