2

I am working on a quiz using firebase and angular. At the end of the quiz i want to save a new property to the users object using this code:

function saveTopscore() {
    var ref = new Firebase(CONSTANTS.FIREBASE_URL + 'users/' + User.user.$id + '/');
    var userObject = $firebaseObject(ref);
    userObject.topscore = User.totalCorrect;
    userObject.$save().then(function(ref) {
        console.log("worked");
    }, function(error) {
        console.log(error);
    });
}

And it works, but it also overwrite all the properties of the user object. So the object used to hold like a name, username, email, password etc, but when i push the topscore it becomes the only property of the object. Why?

2 Answers 2

8

Why are you using $firebaseObject here? That is only useful if you three-way bind it to an Angular view. If you're in regular JavaScript code, just stick to Firebase's regular JavaScript SDK.

In this case, just do:

ref.update({ topscore: User.totalCorrect });
2
  • Thanks that worked! I did it because it said so in the api docs. But this is much easier.
    – idontknow
    Apr 23, 2015 at 22:04
  • 5
    I'm fairly positive the API docs don't say to use $bindTo or any AngularFire bindings if you aren't going to synchronize the data for use in a view or service. (hint: I wrote them)
    – Kato
    Apr 24, 2015 at 15:48
1

As @Frank said, use the JavaScript SDK.

I wanted to clarify the original context (using $firebaseObject) in case anyone else refers to this question in the future.

  • The original symptom is happening because you're setting a single parameter of the object and saving it before the object (and its already-existing children) have been fully downloaded.
  • So, at the time when you call .$save(), userObject will (most likely) only have its topscore parameter set, and calling .$save() will effectively remove all of the other pre-existing children at the Firebase ref.

You could avoid this using .$loaded() to wait until the object has fully downloaded before modifying and/or updating it...

EDIT: However, as the Intro to AngularFire guide states,

The $loaded() method should be used with care as it's only called once after initial load. Using it for anything but debugging is usually a poor practice.

If you were to ignore this warning and use $loaded() anyway, that implementation could look like this:

Note - ANTIPATTERN: This is not a recommended practice.

var ref = new Firebase(fbUrl + '/users/' + User.user.$id + '/');
var userObject = $firebaseObject(ref);
// wait until userObject has been downloaded, then modify & save it.
userObject.$loaded().then(function(){
  userObject.topscore = User.totalCorrect;
  userObject.$save().then(function(ref) {
      console.log("Saved");
  }, function(error) {
      console.log(error);
  });
});

As Frank said in the comments to this answer,

"If you're using $loaded() straight after $firebaseObject (or $firebaseArray), you're probably doing something wrong."

6
  • Hey Sinan. Your approach works too. But I have started using a rule: "if you're using $loaded() straight after $firebaseObject (or $firebaseArray), you're probably doing something wrong". We see way too much use of $loaded() and I'm going to show why people don't need it where I can. ;-) Still... you solution definitely works too. Apr 23, 2015 at 22:56
  • Hi Frank. That's a great point, and that's a great rule! Thanks for clarifying. I was hoping to explain the process of how data is downloaded. I now see in Intro to AngularFire that using $loaded() for something other than debugging is considered poor practice. If $loaded() is being overused, any plans for addressing that? I think it would be awesome to have a list of functional anti-patterns as a reference.
    – sbolel
    Apr 23, 2015 at 23:26
  • 1
    That new documentation you found is one way we try to be more explicit. But it remains tricky. We often tell people to simply bind the $firebaseObject to an element in their view. It will accomplish what they try to do (debug that/what data gets retrieved) with a lot less code, which is what AngularFire is all about. Apr 23, 2015 at 23:44
  • 2
    Great discussion and great ideas. Sinan, if you run across anything you think might be a good usage or antipattern, email it in and we'll add to docs.
    – Kato
    Apr 24, 2015 at 15:49
  • 1
    @SinanBolel I think why people keep making the above mistake and will probably choose $loaded instead of simply using the SDK update is the SDK update does not return a promise... But firebaseObject will. So use it in service we do not need go tho the trouble to create a $q and return promise back to controller. So I think the $loaded method is a lot more straight forward in Angular environment (correct me is I am wrong). But Frank's statement of "you're probably doing something wrong" does scary me to do it this way now...
    – Hugh Hou
    Jun 16, 2015 at 23:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.