cching
cching

Reputation: 799

Firebase TypeError: childSnapshot.child(...).set is not a function

I have a node file that reads data for each child in Firebase.

On each child, I want to set the value of a specific child, but I receive a TypeError that "set" is not a function.

I can access the value of that child, and print it's key, but am not sure why I can not call set.

Here is a snapshot of my code:

myFirebaseRef.once("value", function(snapshot) {
  snapshot.child("Locations").forEach(function(childSnapshot) {
    var startTime = childSnapshot.child("Start Time").val();
    var endTime = childSnapshot.child("End Time").val();
    var currentTime = getTimeStr();

    if(startTime == currentTime) {
      console.log("inside start time");
      childSnapshot.child("Is Available").set('True');
    }

    if(endTime == currentTime) {
      console.log("inside end time");
      childSnapshot.child("Is Available").set('False');
    }
  });
});     

Upvotes: 1

Views: 2263

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 600131

Snapshots are an immutable representation of the data in the database at the moment the snapshot was taken. For that reason you cannot set() a value on a snapshot.

What you can do is set a new value to the same location in the database:

childSnapshot.childSnapshotd("Is Available").ref().set('True')

Then you will get a new value event, which will contain a snapshot with the new value.

Upvotes: 2

Related Questions