不调用我的错误回调函数角资源 [英] angular resource not invoking my error callback function

查看:124
本文介绍了不调用我的错误回调函数角资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在这几个小时,我想不通为什么角是不是触发我的错误回调时,我的导轨后端提出了一个正确的错误。我使用的角度1.2.0rc1。

根据文档:

非GET下课的行动:Resource.action([参数],POSTDATA,[成功],[错误])

我就是一个保存产品操作过程中使用它在我的角度控制器:

  $ scope.saveProduct =功能(产品){  如果(product.id){
    Product.update({ID:product.id},{产品:产品},功能(数据){
      的console.log('成功处理)
    },函数(){
      的console.log('处理错误')//这是从来没有的输出!    });
  }}

下面是资源的定义:

  angular.module('sellbriteApp.resources')。工厂('产品',函数($资源){
  返回$资源('/ API /产品/:身份证',{ID:@id},
    {
      创造:{方法:POST},
      索引:{方法:GET,IsArray的:真正},
      秀:{方法:GET,IsArray的:假},
      更新:{方法:把'},
      灭:{方法:'删除'}
    }
  );
});

下面是我的轨控制器:

  DEF更新
  做的respond_to |格式|
    如果@ product.update(product_params)
      format.html {redirect_to的通知:'产品已成功更新 }
      format.json {渲染的产品/ show.json.jbuilder',状态:接受}
    其他
      format.html {渲染动作:'编辑'}
      format.json {渲染JSON:@ product.errors,状态:unprocessable_entity}
    结束
  结束
结束

Rails的返回试图保存一个产品和SKU重复时,422的状态,我想显示在前端的错误味精。

我期望的角度应该执行在更新呼叫提供的错误处理功能,但我不能走到这一步。相反,在我的控制台我看到:

类型错误:无法与一个无用的堆栈跟踪读未定义属性数据:

 类型错误:无法读取属性'数据'的未定义
在$ http.then.value $解决。(HTTP://本地主机:9000 / bower_components /角资源/角resource.js:477:32)
在wrappedCallback(HTTP://本地主机:9000 / bower_components /角/ angular.js:9042:59)
在wrappedCallback(HTTP://本地主机:9000 / bower_components /角/ angular.js:9042:59)
在http://本地主机:9000 / bower_components /角/ angular.js:9128:26
在Object.Scope $的eval(HTTP://本地主机:9000 / bower_components /角/ angular.js:9953:28)
在Object.Scope $摘要。(HTTP://本地主机:9000 / bower_components /角/ angular.js:9809:23)
。在对象$代表.__原__ $摘要(小于匿名> 844:31)。
。在Object.Scope $应用(HTTP://本地主机:9000 / bower_components /角/ angular.js:10039:24)
。在对象$代表.__原__ $应用(小于匿名> 855:30)
在完成(HTTP://本地主机:9000 / bower_components /角/ angular.js:6542:45)

我是什么失踪?

更新:

显然,这HTTP拦截器有关。如果我评论此code时,误差函数被调用。我从一些地方else和修改了它,以便将用户重定向到sign_up页面,如果他们打的时候都不会在登录一个导轨API复制此片段。它必须是干扰,但我不知道我应该怎么修复吧。

 的App.config(['$ httpProvider',函数($ httpProvider){
  $ httpProvider.responseInterceptors.push('securityInterceptor');
}]);App.factory('securityInterceptor',['$喷油器','$的位置,$的CookieStore',函数($喷油器,$位置$的CookieStore){  复位功能(承诺){
    变量$ HTTP = $ injector.get('$ HTTP');
    返回promise.then(NULL,函数(响应){
      如果(response.status === 401){
        $ cookieStore.remove('_ angular_devise_merchant');
        toastr.warning('您已注销');
        $ location.path('/ sign_in');
      }
    });
  };
}]);


解决方案

您需要拒绝在拦截的承诺为好,否则其视为如果你已经'处理'异常。

所以:

  App.factory('securityInterceptor',['$喷油器','$的位置,$的CookieStore','$ Q',函数($喷油器,$位置$的CookieStore,$ q){  复位功能(承诺){
    变量$ HTTP = $ injector.get('$ HTTP');
    返回promise.then(NULL,函数(响应){
      如果(response.status === 401){
        $ cookieStore.remove('_ angular_devise_merchant');
        toastr.warning('您已注销');
        $ location.path('/ sign_in');
      }
      返回$ q.reject(响应);
    });
  };
}]);

I've been at this for hours and I can't figure out why angular is not triggering my error call back when my rails back-end raises a proper error. I'm using angular 1.2.0rc1.

According to the documentation:

non-GET "class" actions: Resource.action([parameters], postData, [success], [error])

And I'm using it in my angular controller during a save product operation:

$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!

    });
  } 

}

Here is the resource definition:

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

Here is my rails controller:

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

Rails returns a 422 status when attempting to save a product with a duplicate sku, and I want to display an error msg on the front end.

I would expect that angular should execute the error handling function provided in the update call, but I can't get that far. Instead in my console I see:

TypeError: Cannot read property 'data' of undefined with an unhelpful stacktrace:

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

What am I missing?

UPDATE:

Apparently this http interceptor is related. If I comment this code out, the error function is called. I had copied this snippet from some where else and modified it in order to redirect a user to the sign_up page if they hit a rails api when they are not logged in. It must be interfering, but I'm not sure how I should fix it.

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


}]);

解决方案

You need to reject the promise in your interceptor as well, otherwise its considered as if you've 'handled' the exception.

So:

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


}]);

这篇关于不调用我的错误回调函数角资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆