3

I have a class which I'm saving to Firebase using update().

Is it possible to prevent certain fields (known by name) of the object being saved, from being saved to firebase db?

Think like transient in java.

I mean without using JS delete operator.

KarolDepka
  • 7,188
  • 9
  • 37
  • 55

1 Answers1

6

When you call update(), Firebase will change the value of each property (or path) that you've specific in the object you pass in. If you don't want a specific property to be used, don't pass it in.

If you have an existing object and you want a copy that excludes a few fields:

Or:

var obj = { a: 1, b: 2, c: 3, d: 4, e: { f: 5 } }
var updates = {};
Object.keys(obj).forEach((key) => {
  if (key !== "c") updates[key] = obj[key];
});
ref.update(updates);
Frank van Puffelen
  • 418,229
  • 62
  • 649
  • 645
  • Thanks. Upvoted. Is there something like "filter"/"mutator" functions that could be applied when "serializing" to Firebase? That would a useful feature. – KarolDepka Jul 23 '17 at 11:58
  • @KarolDepka If this answer helped, please accept it instead of upvoting so it can help others as well. – Jay Jul 23 '17 at 12:12