googlearchive/firebase-util

$value as a field dependency

Opened this issue · 3 comments

Is using $value as a field dependency supported? I'm running into issues when the value itself changes, with the following error:

FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read property 'ref' of null
    at Object.i.update (https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.min.js:9:22457)
    at https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.min.js:9:18531
    at https://cdn.firebase.com/js/client/2.2.3/firebase.js:49:642
    at Cb (https://cdn.firebase.com/js/client/2.2.3/firebase.js:46:165)
    at yb (https://cdn.firebase.com/js/client/2.2.3/firebase.js:22:216)
    at zb (https://cdn.firebase.com/js/client/2.2.3/firebase.js:21:1259)
    at Ih.h.Fb (https://cdn.firebase.com/js/client/2.2.3/firebase.js:202:287)
    at vh.h.Hd (https://cdn.firebase.com/js/client/2.2.3/firebase.js:189:266)
    at jh.Hd (https://cdn.firebase.com/js/client/2.2.3/firebase.js:180:364)
    at bh.tg (https://cdn.firebase.com/js/client/2.2.3/firebase.js:178:281) 
firebase.js:46 Uncaught TypeError: Cannot read property 'ref' of null

Here is the code I'm using:

var baseRef = new Firebase("https://dev.firebaseio.com");
new Firebase.util.NormalizedCollection(
  [baseRef.child("reservations"), "res"],
  [baseRef.child("reservations_guests"), "rg"],
  [baseRef.child("guests"), "g", "rg.$value"]
).select(
  {"key":"res.$value","alias":"reservation"},
  {"key":"rg.$value","alias":"guestId"},
  {"key":"g.$value","alias":"guest"}
).ref();

And here is a sample of what that database looks like:

reservations: {
  resId1: {
    status: 'arrived'
  },
  resId2: {
    status: 'outstanding'
  }
},
reservations_guests: {
  resId1: 'g140'
  resId2: 'g141'
},
guests: {
  g140: {
    name: 'Guest Number 1'
  },
  g141: {
    name: 'Guest Number 2'
  }
}

If I, for example, change reservations_guests/resId1 to g141, I get the error as shown above.

Seems like a bug at first glance. You may be able to get more details by using the unminified files (the Firebase client library's unminified version is firebase-debug.js)

Here's a plunker http://plnkr.co/edit/ijKyuzPhKYMIs09kzzyr. Angular's used just for display purposes. Otherwise just using unminified versions of Firebase and firebase-util.

This is the output after performing the change I mentioned above:

Record.ValueEventManager.update: url=https://bb-testing.firebaseio.com/reservations_guests/resId1, loadCompleted=true
AbstractRecord._trigger: event=value, id=null, snaps=3, prev=null
RecordList._notify: event=child_changed, key=resId1, prev=%s
Record created resId1 [https://bb-testing.firebaseio.com/reservations/resId1][https://bb-testing.f…o.com/reservations_guests/resId1][https://bb-testing.firebaseio.com/guests]
AbstractRecord._trigger: event=child_changed, id=resId1, snaps=%d, prev=%s
RecordList._notifyValue: snap_keys=resId1,resId2, loadComplete=true
AbstractRecord._trigger: event=value, id=null, snaps=2, prev=null
Record.Dyno: stopped monitoring https://bb-testing.firebaseio.com/guests/g141

firebase-debug.js:4526 FIREBASE WARNING: Exception was thrown by user callback. TypeError: Cannot read property 'ref' of null
    at Object.ValueEventManager.update (https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.js:1576:79)
    at https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.js:1688:7
    at ns.wrapper.fb.core.view.ValueEventRegistration.getEventRunner (https://cdn.firebase.com/js/client/2.2.7/firebase-debug.js:5066:10)
    at Object.ns.wrapper.fb.core.util.exceptionGuard (https://cdn.firebase.com/js/client/2.2.7/firebase-debug.js:4806:5)
    at ns.wrapper.fb.core.view.EventList.raise (https://cdn.firebase.com/js/client/2.2.7/firebase-debug.js:3402:20)
    at ns.wrapper.fb.core.view.EventQueue.raiseQueuedEventsMatchingPredicate_ (https://cdn.firebase.com/js/client/2.2.7/firebase-debug.js:3374:29)
    at ns.wrapper.fb.core.view.EventQueue.raiseEventsForChangedPath (https://cdn.firebase.com/js/client/2.2.7/firebase-debug.js:3362:8)
    at ns.wrapper.fb.core.Repo.setWithPriority (https://cdn.firebase.com/js/client/2.2.7/firebase-debug.js:11810:20)
    at ns.wrapper.Firebase.set (https://cdn.firebase.com/js/client/2.2.7/firebase-debug.js:12843:13)
    at vm.changeGuest (http://run.plnkr.co/ewSEE25pxjCCEQK9/script.js:52:53) 
firebase-debug.js:4811 Uncaught TypeError: Cannot read property 'ref' of null

firebase-util.js:1576 contains the following

util.log('Record.ValueEventManager.update: url=%s, loadCompleted=%s', snap.ref().toString(), this.loadCompleted);

If I comment this logging line out and run the test again, the error is taking place in FieldMap#extractData

TypeError: Cannot read property 'ref' of null
    at Object.FieldMap.extractData (https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.js:386:53)
    at https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.js:1412:18
    at https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.js:4204:24
    at Array.forEach (native)
    at Object.util.each (https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.js:4295:31)
    at Object.util.map (https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.js:4203:8)
    at AbstractRecord.util.inherits.mergeData (https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.js:1411:45)
    at Object.NormalizedSnapshot.val (https://cdn.firebase.com/libs/firebase-util/0.2.4/firebase-util.js:920:22)
    at http://run.plnkr.co/ewSEE25pxjCCEQK9/script.js:14:39
    at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.16/angular.min.js:139:119

Great debugging! And thanks for the plunkr. Should make it easy to fix up. First step will be trying this against the latest master branch. I seem to recall a similar bug fixed a few weeks back, and this may be corrected as well (but I have doubts; I think they are just similar).