angularjsangularjs-resource

angular.js:14195 Error: [$resource:badmember] Dotted member path "@" is invalid


I'm using angular.resource js

There I'm trying to access get service which has query param triggered from a form search.

If user search with only "@" in input field, it goes as query param which starts with "@" character then getting above exception

Thanks in advance.

Because of below code in angular.resource js

// Helper functions and regex to lookup a dotted path on an object
// stopping at undefined/null.  The path must be composed of ASCII
// identifiers (just like $parse)
var MEMBER_NAME_REGEX = /^(\.[a-zA-Z_$@][0-9a-zA-Z_$@]*)+$/;

function isValidDottedPath(path) {
  return (path != null && path !== '' && path !== 'hasOwnProperty' &&
      MEMBER_NAME_REGEX.test('.' + path));
}

function lookupDottedPath(obj, path) {
  if (!isValidDottedPath(path)) {
    throw $resourceMinErr('badmember', 'Dotted member path "@{0}" is invalid.', path);
  }
  var keys = path.split('.');
  for (var i = 0, ii = keys.length; i < ii && angular.isDefined(obj); i++) {
    var key = keys[i];
    obj = (obj !== null) ? obj[key] : undefined;
  }
  return obj;
}

Solution

  • If you really want ot make this as your url params. Try escape the @ char as this has special meaning in $resource library

    Normally it happens when we have only @ as the value, where it searches for the value of string after "@" in the request body. As it is a get method, where angular resource ignores it. Very weird behavior.

    AngularJs doc: https://code.angularjs.org/1.5.11/docs/api/ngResource/service/$resource

    If the parameter value is prefixed with @, then the value for that parameter will be extracted from the corresponding property on the data object (provided when calling a "non-GET" action method). For example, if the defaultParam object is {someParam: '@someProp'} then the value of someParam will be data.someProp. Note that the parameter will be ignored, when calling a "GET" action method (i.e. an action method that does not accept a request body)

    So before calling any method in resource try escape the @ symbol:

    paramValue = paramValue.replace(/@/gi, '\\@');
    

    And again you can remove the scape before api call happens in request method of interceptor service for $httpProvider.

    configParams = configParams.replace(/\\@/gi, '@');
    

    Let me know if need any more help.

    Thanks