I am trying to reorder (swap) children by their numerical priority. Here's my attempt:
var swapChildrenByPriority = function(url, child1Priority, child2Priority){
var ref = new Firebase(url);
var child1, child2;
ref.on('value', function(snap){
snap.forEach(function(child){
if(child.getPriority() === child1Priority){
child1 = child;
};
if(child.getPriority() === child2Priority){
child2 = child;
};
});
});
ref.child(child1.name()).setPriority(child2.getPriority());
ref.child(child2.name()).setPriority(child1.getPriority());
};
As you can imagine, this doesn't work. Chrome's console throws errors at me when this function is called:
TypeError: Cannot call method 'name' of undefined
Uncaught TypeError: Cannot call method 'name' of undefined
It looks as if the children have no priority set, when they do. If I import my data in Forge, all children have the property ".priority" : 5.0.
And when I call the function yet again, just to make sure, I get this one:
TypeError: Object https://test.firebaseio.com/testPriority has no method 'child'
What is the right way to do this in firebase?
Also, how to do the same in AngularFire, so that the data in the scope of a directive that is using angularFire service gets reordered accordingly, and the view gets refreshed?
I very much appreciate your time,
regards
Jared