10

I'm trying to update a field from a snapshot, but even though the snapshot is not null and printed correctly, I can't seem to use ref.update() on it. I tried to follow this answer. What am I missing here?

My code:

 ref.limitToLast(1).on('child_added', function(snapshot) {
    console.log(snapshot.val());
    var serial_number = String(snapshot.child("serial").val()); // 
    console.log(serial_number);
    snapshot.ref().update({ signed: 'true' });
// ...
}

Output:

output

Community
  • 1
  • 1
Mr. Phil
  • 1,099
  • 2
  • 14
  • 31

1 Answers1

16

The problem is that the answer you are referencing uses the previous version of Firebase and whilst the API is very close to the current version, there are a few differences.

There is a guide that discusses the changes and what needs to be done when upgrading from Firebase version 2 and version 3.

In particular, many no-argument getters have been changed to read-only properties:

BEFORE

// Reference
var key = ref.key();
var rootRef = ref.root();
var parentRef = ref.parent();

// Query
var queryRef = query.ref();

// DataSnapshot
ref.on("value", function(snapshot) {
  var dataRef = snapshot.ref();
  var dataKey = snapshot.key();
});

AFTER

// Reference
var key = ref.key;
var rootRef = ref.root;
var parentRef = ref.parent;

// Query
var queryRef = query.ref;

// DataSnapshot
ref.on("value", function(snapshot) {
  var dataRef = snapshot.ref;
  var dataKey = snapshot.key;
});
cartant
  • 57,105
  • 17
  • 163
  • 197
  • cartant had already written the solution in a comment, but since your answer is right I have upvoted and accepted. – Mr. Phil Nov 07 '16 at 11:52
  • @Mr.Phil Yeah, it was my comment. I wrote the comment late last night - my time - but didn't type up a somewhat better formatted answer until this morning. – cartant Nov 07 '16 at 11:55