1

I don't find the way to update an item (You can find all the code here) I tried with this code

app.factory('Items', function($firebase,FIREBASE_URL) {
    var ref = new Firebase(FIREBASE_URL);
    var items = $firebase(ref.child('items')).$asArray();
    var Item = {
        all: function () {
          return items;
        },
        create: function (item) {
          return items.$add(item);
        },
        get: function (itemId) {
          return $firebase(ref.child('items').child(itemId)).$asObject();
        },
        update: function (itemId, item) {
          return $firebase(ref.child('items').child(itemId)).update(item);
        },
        delete: function (item) {
          return items.$remove(item);
        }
    };
    return Item;
});

but it doesn't work. Can you help me, please ?

I've tried after the comment of Frank van Puffelen to

update: function (item) {
    return items.$save(item);
}

but it doesn't work and get me

Object { status=2, value="Invalid record; could d...ts key: [object Object]"}

You can see a workish example at :

https://lalista.firebaseapp.com

Community
  • 1
  • 1
Whisher
  • 31,320
  • 32
  • 120
  • 201

2 Answers2

4

If you have an explicit update function, you're not using Angular's/AngularFire's two-way/three-way data binding. In that case you might as well bypass AngularFire for the update and call Firebase's JavaScript API directly:

 update: function (itemId, item) {
     return ref.child('items').child(itemId).set(item);
 }

Note that I call set instead of update, because it looks like you're trying to replace any existing data Firebase might be storing for the item.

Firebase's JavaScript API and AngularFire play nicely together here, so you don't have to worry about the data getting out of sync.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • thanks again :) for the point but it's my fault I mixed up plain firebase with angularfire so what's the way to do update with angularfire ? – Whisher Oct 29 '14 at 20:53
  • 1
    I tried to use like your reply but I've got Error: Firebase.set failed: First argument contains an invalid key ($$conf). Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]" – Whisher Oct 30 '14 at 10:10
  • That sounds like you did `update('$$conf')` or something like that. Can you share the code snippet you used? – Frank van Puffelen Oct 30 '14 at 11:25
  • You can see the workish example https://lalista.firebaseapp.com now I've set items.$save(item) but you've got all the code. – Whisher Oct 30 '14 at 12:18
  • Replace this `console.log(Items.update(item.$id,item));` with `Items.child(item.$id).update(item);`. Please use the documentation : https://www.firebase.com/docs/web/api/firebase/update.html – Frank van Puffelen Oct 30 '14 at 12:51
  • Sorry but I miss the point Items doesn't have a child method and the signature of Items.update is Items.update(itemId,item) – Whisher Oct 30 '14 at 13:46
  • Then you're still using AngularFire, where the whole point of my answer was to bypass AngularFire and simply use Firebase's regular JavaScript API. – Frank van Puffelen Oct 30 '14 at 13:57
  • @FrankvanPuffelen I think what Whisher was saying is that any data that comes by way of AngularFire, will inherently contain keys that regular ole Firebase won't accept ($conf, $id etc), metadata on the object. That is the problem. – Ryan Jun 04 '16 at 06:52
2

For anybody coming to this question using the new AngularFire APIs $firebaseArray and $firebaseObject, the methods are:

$firebaseArray.$save(recordOrIndex)

var list = $firebaseArray(ref);
    list[2].foo = "bar";
list.$save(2).then(function(ref) {
  ref.key() === list[2].$id; // true
});

$firebaseObject.$save()

var obj = $firebaseObject(ref);
obj.foo = "bar";
obj.$save().then(function(ref) {
  ref.key() === obj.$id; // true
}, function(error) {
  console.log("Error:", error);
});
Alex Ross
  • 3,729
  • 3
  • 26
  • 26