Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 528
KarolDepka Avatar asked Jan 21 '26 02:01

KarolDepka


1 Answers

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:

  • clone a js object except for one key

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);
like image 104
Frank van Puffelen Avatar answered Jan 23 '26 14:01

Frank van Puffelen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!