0
votes

Based on the AngularFire documentation I'm trying to set $priority on an object, then use $save() to update the data on Firebase.
The result I get instead is the object getting overwritten as empty (it's entry in Firebase). Here is my code

    var ref = new Firebase(firebaseListPath);
    var myList = $firebase(ref).$asArray();

    myList.$add({
      created: Firebase.ServerValue.TIMESTAMP,
      user: userId
    }).then(function(newItemRef){
      var newItemObj = $firebase(newItemRef).$asObject();
      newItemObj.$priority = userId;
      newItemObj .$save();
    });
1
Can you try $firebase(ref).$update(netItemObj.$id, newItemObj)? firebase.com/docs/web/libraries/angular/…. Or pass $priority or .priority straight into $add, instead of waiting for the promise to fulfil. - Frank van Puffelen

1 Answers

1
votes

Try passing the priority directly into $add:

myList.$add({
   created: Firebase.ServerValue.TIMESTAMP,
   user: userId,
   $priority: userId
});

There is also no reason to call $asArray and $asObject on the same synchronized data (it's already synchronized). This would work in your current model, but be unnecessary:

myList.$add({
  created: Firebase.ServerValue.TIMESTAMP,
  user: userId
}).then(function(newItemRef){
  var item = myList.$getRecord(newItemRef.name());
  item.$priority = userId;
  myList.$save(item);
});

However, there is really no reason to be using AngularFire here. If you aren't binding anything to the view, then you can simply make the call directly to the ref:

var ref = new Firebase(firebaseListPath);
ref.push().setWithPriority({ ... }, userId);