Ruby on Rails角度资源为何不触发我的错误回调函数?

2026-04-11 16:282阅读0评论SEO问题
  • 内容介绍
  • 相关推荐

本文共计695个文字,预计阅读时间需要3分钟。

Ruby on Rails角度资源为何不触发我的错误回调函数?

我在最近这几个小时里遇到了问题,无法清除我Rails后端的一个正确错误,而Angular则不会触发我的错误回调。我正在使用Angular 1.2.0-rc1,根据文件信息,问题出在非GET操作的Resource.action([param])上。

我已经在这几个小时了,我无法弄清楚为什么当我的rails后端引发一个正确的错误时,angular不会触发我的错误回调.我正在使用角度1.2.0rc1.

根据文件:

非GET“类”操作:Resource.action([parameters],postData,[success],[error])

我在保存产品操作期间在角度控制器中使用它:

$scope.saveProduct = function(product){ if (product.id) { Product.update({id: product.id},{product: product}, function(data){ console.log('handle success') }, function(){ console.log('handle error') //THIS IS NEVER OUTPUT! }); } }

这是资源定义:

Ruby on Rails角度资源为何不触发我的错误回调函数?

angular.module('sellbriteApp.resources').factory('Product', function ($resource) { return $resource('/api/products/:id', { id: "@id" }, { 'create': { method: 'POST' }, 'index': { method: 'GET', isArray: true }, 'show': { method: 'GET', isArray: false }, 'update': { method: 'PUT' }, 'destroy': { method: 'DELETE' } } ); });

这是我的rails控制器:

def update respond_to do |format| if @product.update(product_params) format.html { redirect_to [:edit, @product], notice: 'Product was successfully updated.' } format.json { render 'products/show.json.jbuilder', status: :accepted } else format.html { render action: 'edit' } format.json { render json: @product.errors, status: :unprocessable_entity } end end end

尝试保存具有重复sku的产品时,Rails返回422状态,并且我想在前端显示错误消息.

我希望angular应该执行更新调用中提供的错误处理函数,但我无法做到这一点.而是在我的控制台中,我看到:

TypeError:无法使用无用的堆栈跟踪读取未定义的属性“data”:

TypeError: Cannot read property 'data' of undefined at $localhost:9000/bower_components/angular-resource/angular-resource.js:477:32) at wrappedCallback (localhost:9000/bower_components/angular/angular.js:9042:59) at wrappedCallback (localhost:9000/bower_components/angular/angular.js:9042:59) at localhost:9000/bower_components/angular/angular.js:9128:26 at Object.Scope.$eval (localhost:9000/bower_components/angular/angular.js:9953:28) at Object.Scope.$digest (localhost:9000/bower_components/angular/angular.js:9809:23) at Object.$delegate.__proto__.$digest (<anonymous>:844:31) at Object.Scope.$apply (localhost:9000/bower_components/angular/angular.js:10039:24) at Object.$delegate.__proto__.$apply (<anonymous>:855:30) at done (localhost:9000/bower_components/angular/angular.js:6542:45)

我错过了什么?

更新:

显然这个http拦截器是相关的.如果我将此代码注释掉,则会调用错误函数.我已经从其他地方复制了这个片段并对其进行了修改,以便在用户未登录时点击rails api将用户重定向到sign_up页面.它必须是干扰,但我不知道应该如何解决它.

App.config(['$httpProvider', function ($httpProvider) { $httpProvider.responseInterceptors.push('securityInterceptor'); }]); App.factory('securityInterceptor', ['$injector', '$location', '$cookieStore', function ($injector,$location,$cookieStore) { return function(promise) { var $http = $injector.get('$http'); return promise.then(null, function(response){ if (response.status === 401) { $cookieStore.remove('_angular_devise_merchant'); toastr.warning('You are logged out'); $location.path('/sign_in'); } }); }; }]); 你需要在拦截器中拒绝承诺,否则它被视为你已“处理”异常.

所以:

App.factory('securityInterceptor', ['$injector', '$location', '$cookieStore', '$q', function ($injector,$location,$cookieStore, $q) { return function(promise) { var $http = $injector.get('$http'); return promise.then(null, function(response){ if (response.status === 401) { $cookieStore.remove('_angular_devise_merchant'); toastr.warning('You are logged out'); $location.path('/sign_in'); } return $q.reject(response); }); }; }]);

本文共计695个文字,预计阅读时间需要3分钟。

Ruby on Rails角度资源为何不触发我的错误回调函数?

我在最近这几个小时里遇到了问题,无法清除我Rails后端的一个正确错误,而Angular则不会触发我的错误回调。我正在使用Angular 1.2.0-rc1,根据文件信息,问题出在非GET操作的Resource.action([param])上。

我已经在这几个小时了,我无法弄清楚为什么当我的rails后端引发一个正确的错误时,angular不会触发我的错误回调.我正在使用角度1.2.0rc1.

根据文件:

非GET“类”操作:Resource.action([parameters],postData,[success],[error])

我在保存产品操作期间在角度控制器中使用它:

$scope.saveProduct = function(product){ if (product.id) { Product.update({id: product.id},{product: product}, function(data){ console.log('handle success') }, function(){ console.log('handle error') //THIS IS NEVER OUTPUT! }); } }

这是资源定义:

Ruby on Rails角度资源为何不触发我的错误回调函数?

angular.module('sellbriteApp.resources').factory('Product', function ($resource) { return $resource('/api/products/:id', { id: "@id" }, { 'create': { method: 'POST' }, 'index': { method: 'GET', isArray: true }, 'show': { method: 'GET', isArray: false }, 'update': { method: 'PUT' }, 'destroy': { method: 'DELETE' } } ); });

这是我的rails控制器:

def update respond_to do |format| if @product.update(product_params) format.html { redirect_to [:edit, @product], notice: 'Product was successfully updated.' } format.json { render 'products/show.json.jbuilder', status: :accepted } else format.html { render action: 'edit' } format.json { render json: @product.errors, status: :unprocessable_entity } end end end

尝试保存具有重复sku的产品时,Rails返回422状态,并且我想在前端显示错误消息.

我希望angular应该执行更新调用中提供的错误处理函数,但我无法做到这一点.而是在我的控制台中,我看到:

TypeError:无法使用无用的堆栈跟踪读取未定义的属性“data”:

TypeError: Cannot read property 'data' of undefined at $localhost:9000/bower_components/angular-resource/angular-resource.js:477:32) at wrappedCallback (localhost:9000/bower_components/angular/angular.js:9042:59) at wrappedCallback (localhost:9000/bower_components/angular/angular.js:9042:59) at localhost:9000/bower_components/angular/angular.js:9128:26 at Object.Scope.$eval (localhost:9000/bower_components/angular/angular.js:9953:28) at Object.Scope.$digest (localhost:9000/bower_components/angular/angular.js:9809:23) at Object.$delegate.__proto__.$digest (<anonymous>:844:31) at Object.Scope.$apply (localhost:9000/bower_components/angular/angular.js:10039:24) at Object.$delegate.__proto__.$apply (<anonymous>:855:30) at done (localhost:9000/bower_components/angular/angular.js:6542:45)

我错过了什么?

更新:

显然这个http拦截器是相关的.如果我将此代码注释掉,则会调用错误函数.我已经从其他地方复制了这个片段并对其进行了修改,以便在用户未登录时点击rails api将用户重定向到sign_up页面.它必须是干扰,但我不知道应该如何解决它.

App.config(['$httpProvider', function ($httpProvider) { $httpProvider.responseInterceptors.push('securityInterceptor'); }]); App.factory('securityInterceptor', ['$injector', '$location', '$cookieStore', function ($injector,$location,$cookieStore) { return function(promise) { var $http = $injector.get('$http'); return promise.then(null, function(response){ if (response.status === 401) { $cookieStore.remove('_angular_devise_merchant'); toastr.warning('You are logged out'); $location.path('/sign_in'); } }); }; }]); 你需要在拦截器中拒绝承诺,否则它被视为你已“处理”异常.

所以:

App.factory('securityInterceptor', ['$injector', '$location', '$cookieStore', '$q', function ($injector,$location,$cookieStore, $q) { return function(promise) { var $http = $injector.get('$http'); return promise.then(null, function(response){ if (response.status === 401) { $cookieStore.remove('_angular_devise_merchant'); toastr.warning('You are logged out'); $location.path('/sign_in'); } return $q.reject(response); }); }; }]);