14

I can use the following to add an object to my Firebase data store:

var uniqueId = {
    name: "a name",
    location: "new york"
}
$scope.myItems.$add(uniqueId).then(function(firebaseId){
    // do something on success
}, function(){
    // do something if call fails
});

The above will add an object into my data store and if the add is successful, an ID generated by Firebase is returned. The object I just added is saved under this key.

Is there a way for me to specify what the key name is when I add to my data store?

David East
  • 31,526
  • 6
  • 67
  • 82
Lloyd Banks
  • 35,740
  • 58
  • 156
  • 248
  • 2
    Yes there is. Just use `child` and `set`, e.g. `nameRef.child('first').set('Fred');`. See https://www.firebase.com/docs/writing-data.html. In AngularFire that might translate to `$save()`, see https://github.com/firebase/angularFire/blob/12c7e81d1ccde9def274b7eac74f290b6c992e78/angularfire.js#L202. – Frank van Puffelen Jul 03 '14 at 18:49
  • Here is similar question: http://stackoverflow.com/questions/18050856/how-to-set-a-custom-id-when-pushing-a-new-object-into-firebase – Alexander Burakevych Jul 03 '14 at 23:39

4 Answers4

17

Everything in Firebase is a URL.

Take the following URL for example.

https://myapp.firebaseio.com/users/

Let's say we want to create a user with a key of 1 as a child at this location. Our URL would look like this.

https://myapp.firebaseio.com/users/1

To create a user using AngularFire we can create a reference at the users node and call $child(1) to create a reference to that location.

var usersRef = new Firebase('https://myapp.firebaseio.com/users');
var userRef = new Firebase('https://myapp.firebaseio.com/user/1');

$scope.users = $firebase(usersRef);

// these are the same
$scope.userOne = $firebase(userRef);
$scope.userOne = $scope.users.$child(1);

Then we can use $set to store the value of the user at that location.

var usersRef = new Firebase('https://myapp.firebaseio.com/users');
$scope.users = $firebase(usersRef);
$scope.users.$child(1).set({
  first: 'Vincent',
  last: 'Van Gough',
  ears: 1
});

In your case it would be:

var uniqueId = {
    id: 1,
    name: "a name",
    location: "new york"
};
$scope.myItems.$child(uniqueId.id).$set(uniqueId);

Remember that using $set will destroy any previous data at that location. To non-destructively update the values use $update.

Community
  • 1
  • 1
David East
  • 31,526
  • 6
  • 67
  • 82
  • I knew about the `$child` + `$set` combination. I just didn't know you had to chain the two together to make it work. If you call `$child` then `$set` with two separate commands, changes won't go through – Lloyd Banks Jul 07 '14 at 14:06
  • That's because $child returns a $firebase object. You won't have to chain them together if you store the result either in variable or on $scope. – David East Jul 07 '14 at 14:17
  • I have tried this exact same code with my own server but it doesn't work. I always get undefined is not a function referring to $child. I suspect that the new API doesn't include $child. It doesn't even appear in their API documentation: https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire-firebase-set-key-data – Maher Manoubi Oct 07 '14 at 21:57
  • Yes, the API for AngularFire has had major updates since the 0.8 update. `$child` was removed in that version. – David East Oct 08 '14 at 13:30
  • 1
    @DavidEast what's the new equivalent to $child? can't find it when reading here: https://www.firebase.com/blog/2014-07-30-introducing-angularfire-08.html Please help. – Chrillewoodz Jan 26 '15 at 20:03
  • @DavidEast $set seems to be missing from AngularFire too. How can one specify a key for the newly created object in the $firebaseArray? – Alex Kovshovik May 19 '15 at 21:47
9

You can try it like this:

var empsRef = ref.child("employees");

empsRef.child('11111').set({
  lastname: "Lee",
  firstname: "Kang"
});

empsRef.child('22222').set({
  lastname: "Nut",
  firstname: "Dough"
});

The output should look like this:

"employees" : {
  "11111" : {
    "lastname" : "Lee",
    "firstname": "Kang"
  },
  "22222" : {
    "lastname" : "Nut",
    "firstname": "Dough"
  }
}
grg
  • 5,023
  • 3
  • 34
  • 50
Lee
  • 241
  • 3
  • 4
3

It looks like $child and $set have been removed from the AngularFire API.

Here is my workaround.

  1. Listen for $onAuth
  2. Create a new $firebaseObject with the ref as a child of users and the id as the uid (in this case, facebook)
  3. Save the user object

    angular.module("app.services", []).factory("Auth", function ($firebaseAuth, $firebaseObject, FirebaseUrl) {
    var auth = $firebaseAuth(new Firebase(FirebaseUrl));
    
    auth.$onAuth(function (authData) {
        if (authData) {
            var ref = new Firebase(FirebaseUrl + "/users/" + authData.uid);
            var user = $firebaseObject(ref);
            user.name = authData.facebook.displayName;
            user.$save().then(function () {
                console.log(user);
            });
        }
    });
    return auth;
    })
    

I am still struggling to make this testable though...

Alex
  • 952
  • 7
  • 12
0

.set still working in angularfire api

So, try this...

var gT = $scope.ultima[0].guia + 1;                    
var _db = firebase.database().ref('/guias/' + gT);
                      _db.set({
                    'artigo': _dt.artigo,
                    'inicio': $scope.ultima[0].fim + 1,
                    'guia': $scope.ultima[0].guia + 1,
                    'fim': $scope.ultima[0].fim + 1 + (1 * _dt.quantidade),
                    'quantidade': _dt.quantidade,
                });