KarolDepka
KarolDepka

Reputation: 8628

Firebase update() - exclude fields

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.

Upvotes: 3

Views: 917

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599591

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);

Upvotes: 6

Related Questions