mockfirebase icon indicating copy to clipboard operation
mockfirebase copied to clipboard

child_added triggered with partial data

Open nvdbleek opened this issue 8 years ago • 0 comments

The child_added event is triggered with not the complete data tree when you set data on a child that doesn't yet exists (all but the first key of the object is not present yet, and you gat a value event for the rest of the keys). My use case is to use transaction() to make sure that the child is only added when it doesn't yet exists, to make sure that only one client will add the node at that particular key.

If you run the following example:

var Firebase = require('mockfirebase');

var ref = new Firebase.MockFirebase('child_added://');
ref.child('foo').on('child_added', function (snaphot) {
  console.log(snaphot.key() + ' = ' + JSON.stringify(snaphot.val()))
});

ref.child('foo').child('a1').set({ foo: ['baz'], qux: 'norf'} );
ref.flush();

ref.child('foo').child('a2').transaction(function (current) {
   if (current === null) {
     return { foo: ['baz'], qux: 'norf'};
    }
  },
  function (error, committed, snaphot) {
    // console.log(snaphot.key() + ' = ' + JSON.stringify(snaphot.val()))
});
ref.flush();

You get the following output:

a1 = {"foo":{"0":"baz"}}
a2 = {"foo":{"0":"baz"}}

I would have expected to get:

a1 = {"foo":["baz"],"qux":"norf"}
a2 = {"foo":["baz"],"qux":"norf"}

Note the missing qux key in the output.

I tried to delay the sending of the child_added event, because it is send after that the first entry is added. But my attempt breaks a lot of other tests.

Is it possible to point me in the correct direction to make a fix for this issue.

My attempt is pushed as commit https://github.com/nvdbleek/mockfirebase/commit/9c1f099a7db0c7afb51dfb6a74ca7871e8fbb520

Thanks in advance

nvdbleek avatar Oct 21 '15 06:10 nvdbleek