angularfire icon indicating copy to clipboard operation
angularfire copied to clipboard

invalid key $$hashKey: strip $$ vars in $firebaseUtils.toJSON

Open katowulf opened this issue 12 years ago • 4 comments

Users are encountering issues when nesting synchronized objects/arrays inside of other synchronized objects/arrays. Specifically, ng-repeat adds a lot of $$ props onto them which are not being stripped. The error thrown is by our validation checks before sending data on to Firebase:

Error: Firebase.set failed: First argument contains an invalid key ($$hashKey) in property 'items.0'.  Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]"

The simplest answer here is probably to strip any $$ property inside of $firebaseUtils.toJSON(). This would need to be done recursively on child props. Here's a workaround I used successfully today:

//todo-hack will fix this in AngularFire 0.8.1
app.config(function($provide) {
  $provide.decorator('$firebaseUtils', function($delegate) {
    function stripDollarPrefixes(obj) {
      if( angular.isArray(obj) ) {
        obj = obj.slice();
        angular.forEach(obj, function(v,k) {
          obj[k] = stripDollarPrefixes(v);
        });
      }
      else if( angular.isObject(obj) ) {
        obj = angular.extend({}, obj);
        angular.forEach(obj, function (v, k) {
          if (k.charAt(0) === '$') {
            delete obj[k];
          }
          else {
            obj[k] = stripDollarPrefixes(v);
          }
        });
      }
      return obj;
    }
    var toJSON = $delegate.toJSON;
    $delegate.toJSON = function(rec) {
      var json = toJSON(rec);
      angular.forEach(json, function(v,k) {
        if( angular.isObject(v) ) {
          json[k] = stripDollarPrefixes(v);
        }
      });
      return json;
    };
    return $delegate;
  });
});

However, the same issue occurs when using $add on $FirebaseArray and, even though that method calls $firebaseUtils.toJSON, it did not fix the problem in this case. Ensure test units contain checks for this in any write points.

katowulf avatar Aug 22 '14 22:08 katowulf

I stumbled upon this issue a few days ago and preliminary got around it by using thing in things track by thing.id to avoid the $$hashKey (https://docs.angularjs.org/api/ng/directive/ngRepeat)

gjungb avatar Aug 23 '14 09:08 gjungb

Gerd,

Cheers; that's a great solution for things have a unique id of some sort. This assumes that you have an actual thing.id on the children, correct?

For things which are not synchronized objects, like nested arrays--which we discourage but people still utilize--I think this problem will still show up as there is nothing to track by. However, a great workaround for general use cases.

katowulf avatar Aug 23 '14 15:08 katowulf

Looks like it's back again in version AngularFire 0.9.0 and Firebase v2.0.4

oburakevych avatar Dec 18 '14 09:12 oburakevych

Please provide a repro. We've not heard of anyone running into this issue in a long while and I'm not sure how it could possibly have come back in with 0.9.0 since that didn't even touch this code.

jwngr avatar Dec 18 '14 17:12 jwngr